def generate_network():#num_parents, num_leaves, approx_num_total_nodes, approx_connectivity):
	"""Generates the network on page 362 on bishop... Too much work to generate random networks..."""
	#lambda to collapse the list of parent's states into a binary string
	get_parents_binary_string = lambda parents: f.foldl(lambda acc, element: acc<<1 | element.state, 0b0, parents)
	#simply looks up the binary string formed from the parents in the p_map
	p_up = lambda p_map, parents: p_map[get_parents_binary_string(parents)]
	'''
	#simple network for testing purposes
	node1 = Bayesian_node(None, lambda: .4, None, 0)
	node2_p_dict = {0b0:.2, 0b1:.5}
	node2 = Bayesian_node(None, f.partial(p_up, node2_p_dict), [node1], 1)
	node1.children = [node2]

	return Network([node1], {0: node1, 1:node2})'''

	node1 = Bayesian_node(None, lambda: .4, None, 0)
	node2 = Bayesian_node(None, lambda: .7, None, 1)
	node3 = Bayesian_node(None, lambda: .3, None, 2)

	node4_p_dict = {0b000:.2, 0b001:.5, 0b010:.7, 0b100:.43, 0b011:.15, 0b101:.35, 0b110: .6, 0b111:.25}
	node4 = Bayesian_node(None, f.partial(p_up, node4_p_dict), [node1, node2, node3], 3)
	node5_p_dict = {0b00:.9, 0b01:.5, 0b10:.2, 0b11:.1}
	node5 = Bayesian_node(None, f.partial(p_up, node5_p_dict), [node1, node3],4)
	node6_p_dict = {0b1:.85, 0b0:.374}
	node6 = Bayesian_node(None, f.partial(p_up, node6_p_dict), [node4], 5)
	node7_p_dict = {0b00:.19, 0b01:.27, 0b10:.5, 0b11:.333}
	node7 = Bayesian_node(None, f.partial(p_up, node7_p_dict), [node4, node5], 6)

	node1.children = [node4, node5]
	node2.children = [node4]
	node3.children = [node4, node5]
	node4.children = [node6, node7]
	node5.children = [node7]

	return Network([node1,node2,node3], {0: node1, 1: node2, 2: node3, 3:node4, 4:node5, 5:node6, 6:node7})
示例#2
0
def sum(itr):
    """
    sum(iterable) -> number
    
    sum([x0, x1, x2,..., xN]) is equivalent to 0 + x0 + x1 + x2 + ... + xN
    """
    return foldl(add, 0, itr)
示例#3
0
def product(itr):
    """
    product(iterable) -> number
    
    product([x0, x1, x2,..., xN]) is equivalent to 1 * x0 * x1 * x2 * ... * xN
    """

    return foldl(mul, 1, itr)
示例#4
0
def code_from_array(array, format_function):
    """
    Função de Primeira Ordem, pura, onde será aplicado o conceito de Currying.
    Utilização do Lambda e das funções de ordem maior map e foldl
    Essa função é responsável por formatar um Array de código em uma string que será exibida na tela.
    """
    spaces = len(str(len(array)))
    new_array = map(lambda a: "%s%d "+a, array) 
    new_array = format_function(new_array)
    code = foldl(lambda a,b: a+b, "", new_array)
    return code
示例#5
0
  def get_drawing(self):
    def add_child_drawing(drawing, child):
      child_drawing = child.get_drawing()
      if child is self.focused_child:
        l, t, _, _ = child_drawing.boundingbox
        border = drawings.Move(
          drawings.Rectangle(
            (child_drawing.boundingbox.width() + 4, child_drawing.boundingbox.height() + 4),
            (0.3, 0.3, 0.8),
            fill='stroke'
          ),
          (l - 2, t - 2)
        )
        child_drawing = drawings.Atop(border, child_drawing)
      return drawings.Atop(child_drawing, drawing)

    return foldl(add_child_drawing, self.children, drawings.Empty())
    return res
print fact5(6)

#Lazy Python programmer
def fact6(x):
    return x > 1 and x * fact(x - 1) or 1
print fact6(6)

#Lazier Python programmer
f = lambda x: x and x * f(x - 1) or 1
print f(6)

#Python expert programmer
import operator as op
import functional as f
fact7 = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact7(6)

#Python hacker
import sys
@tailcall
def fact8(x, acc=1):
    if x: return fact(x.__sub__(1), acc.__mul__(x))
    return acc
sys.stdout.write(str(fact8(6)) + '\n')

#EXPERT PROGRAMMER
import c_math
fact9 = c_math.fact
print fact(6)