예제 #1
0
파일: tests.py 프로젝트: Gizmo707/fn.py
    def test_zipwith(self):
        zipper = op.zipwith(operator.add)
        self.assertEqual([10,11,12], list(zipper([0,1,2], itertools.repeat(10))))

        zipper = op.zipwith(_ + _)
        self.assertEqual([10,11,12], list(zipper([0,1,2], itertools.repeat(10))))

        zipper = F() << list << op.zipwith(_ + _)
        self.assertEqual([10,11,12], zipper([0,1,2], itertools.repeat(10)))
예제 #2
0
    def test_zipwith(self):
        zipper = op.zipwith(operator.add)
        self.assertEqual([10,11,12], list(zipper([0,1,2], itertools.repeat(10))))

        zipper = op.zipwith(_ + _)
        self.assertEqual([10,11,12], list(zipper([0,1,2], itertools.repeat(10))))

        zipper = F() << list << op.zipwith(_ + _)
        self.assertEqual([10,11,12], zipper([0,1,2], itertools.repeat(10)))
예제 #3
0
print(ioMonad)


def createIO(word) -> IO:
    return IO(word)


def toUpper(word) -> IO:
    return IO(word.upper())


def appendValue(word) -> IO:
    return IO(word + " at " + str(datetime.now().microsecond))


foo = createIO("Hello") | (lambda word: toUpper(word) |
                           (lambda contents: appendValue(contents)))

time.sleep(2)
print("now:%s" % str(datetime.now().microsecond))
time.sleep(2)
print(foo.value)

from fn import _
from fn.op import zipwith
from itertools import repeat

assert list(map(_ * 2, range(5))) == [0, 2, 4, 6, 8]
assert list(filter(_ < 10, [9, 10, 11])) == [9]
assert list(zipwith(_ + _)([0, 1, 2], repeat(10))) == [10, 11, 12]
예제 #4
0
# -*- coding:utf-8 -*-
from pprint import pprint

from fn import _
from fn.op import zipwith
from itertools import repeat

""" 1、Scala-style lambdas definition """
pprint(list(map(_ * 2, range(5))))  # [0, 2, 4, 6, 8]
pprint(list(filter(_ < 10, [9, 10, 11])))  # [9]
pprint(list(zipwith(_ + _)([0, 1, 2], repeat(10))))  # [10, 11, 12]
# Attention "_"
pprint(_ + 2)  # "(x1) => (x1 + 2)"
pprint(_ + _ * _)  # "(x1, x2, x3) => (x1 + (x2 * x3))"


""" 2、Persistent data structures """

예제 #5
0
from operator import add

from fn import Stream

# f = Stream()
#
# fib = f<<[0,1]<<map(add,f,drop(1,f))
#
# print(list(take(10,fib)))
#
# print(fib[20])
#
# print(list(fib[30:35]))

from fn.func import curried
from fn.op import zipwith


@curried
def sum5(a, b, c, d, e):
    return a + b + c + d + e


print(sum5(1)(2)(3)(4)(5))

print(sum5(1, 2, 3)(4, 5))

from itertools import repeat

print(list(zipwith(_ + _)([0, 1, 2], repeat(10))))