From 6620e730e12d935e72c2e7860bedd49126b78177 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 15:51:20 +0200 Subject: [PATCH 1/7] Add compose --- snippets/compose.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/compose.md diff --git a/snippets/compose.md b/snippets/compose.md new file mode 100644 index 000000000..08f90736a --- /dev/null +++ b/snippets/compose.md @@ -0,0 +1,24 @@ +--- +title: compose +tags: function,intermediate +--- + +Performs right-to-left function composition. + +Use `reduce()` to perform right-to-left function composition. +The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. + +```py +from functools import reduce + +def compose(*fns): + return reduce(lambda f, g: lambda *args: f(g(*args)), fns) +``` + +```py +add5 = lambda x: x + 5 +multiply = lambda x, y: x * y +multiply_and_add_5 = compose(add5, multiply) + +multiply_and_add_5(5, 2) # 15 +``` From 37f433feb7e7ce378980e9c3489c8da88128f5a2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 15:51:26 +0200 Subject: [PATCH 2/7] Add compose right --- snippets/compose_right.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/compose_right.md diff --git a/snippets/compose_right.md b/snippets/compose_right.md new file mode 100644 index 000000000..696f89da6 --- /dev/null +++ b/snippets/compose_right.md @@ -0,0 +1,24 @@ +--- +title: compose_right +tags: function,intermediate +--- + +Performs left-to-right function composition. + +Use `reduce()` to perform left-to-right function composition. +The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. + +```py +from functools import reduce + +def compose_right(*fns): + return reduce(lambda f, g: lambda *args: g(f(*args)), fns) +``` + +```py +add = lambda x, y: x + y +square = lambda x: x * x +add_and_square = compose_right(add,square) + +add_and_square(1, 2) # 9 +``` From 2973799a7d6f888110f64a35a7f28cdef0c71955 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 16:14:50 +0200 Subject: [PATCH 3/7] Add curry --- snippets/curry.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/curry.md diff --git a/snippets/curry.md b/snippets/curry.md new file mode 100644 index 000000000..855cd81a6 --- /dev/null +++ b/snippets/curry.md @@ -0,0 +1,22 @@ +--- +title: curry +tags: function,intermediate +--- + +Curries a function. + +Use `partial()` to return a new partial object which behaves like `fn` with the given arguments, `args`, partially applied. + +```py +from functools import partial + +def curry(fn, *args): + return partial(fn,*args) +``` + +```py +add = lambda x, y: x + y +add10 = curry(sum, 10) + +add10(20) # 30 +``` From 038ed366c1f6529380361042c2290c9faa6e4350 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 16:24:51 +0200 Subject: [PATCH 4/7] Add delay --- snippets/delay.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/delay.md diff --git a/snippets/delay.md b/snippets/delay.md new file mode 100644 index 000000000..d77d11683 --- /dev/null +++ b/snippets/delay.md @@ -0,0 +1,24 @@ +--- +title: delay +tags: function,intermediate +--- + +Invokes the provided function after `ms` milliseconds. + +Use `sleep()` to delay the execution of `fn` by `ms / 1000` seconds. + +```py +from time import sleep + +def delay(fn, ms, *args): + sleep(ms / 1000) + return fn(*args) +``` + +```py +delay( + lambda x: print(x), + 1000, + 'later' +) # prints 'later' after one second +``` From 1daa5e9e85711cc1ef96f161b74b96dc1f2d4034 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 16:49:25 +0200 Subject: [PATCH 5/7] Add check prop --- snippets/check_prop.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/check_prop.md diff --git a/snippets/check_prop.md b/snippets/check_prop.md new file mode 100644 index 000000000..cb98054bf --- /dev/null +++ b/snippets/check_prop.md @@ -0,0 +1,23 @@ +--- +title: function_name +tags: utility,intermediate +--- + +Given a predicate function, `fn`, and a `prop` string, this curried function will then take an object to inspect by calling the property and passing it to the predicate. + +Return a `lambda` function that takes an object and applies the predicate function, `fn` to the specified property. + +```py +def check_prop(fn, prop): + return lambda obj: fn(obj[prop]) +``` + +```py +check_age = check_prop(lambda x: x >= 18, 'age') +user = { + 'name': 'Mark', + 'age': 18 +} + +check_age(user) # True +``` From 79c1d65ea514b2fb1d627f922b55cb4caaf05b65 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 20:17:51 +0200 Subject: [PATCH 6/7] Add unfold --- snippets/unfold.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 snippets/unfold.md diff --git a/snippets/unfold.md b/snippets/unfold.md new file mode 100644 index 000000000..a354cc4a8 --- /dev/null +++ b/snippets/unfold.md @@ -0,0 +1,25 @@ +--- +title: unfold +tags: function,list,advanced +--- + +Builds a list, using an iterator function and an initial seed value. + +The iterator function accepts one argument (`seed`) and must always return a list with two elements ([`value`, `nextSeed`]) or `False` to terminate. +Use a generator function, `fn_generator`, that uses a `while` loop to call the iterator function and `yield` the `value` until it returns `False`. +Use list comprehension to return the list that is produced by the generator, using the iterator function. + +```py +def unfold(fn, seed): + def fn_generator(val): + while True: + val = fn(val[1]) + if val == False: break + yield val[0] + return [i for i in fn_generator([None, seed])] +``` + +```py +f = lambda n: False if n > 50 else [-n, n + 10] +unfold(f, 10) # [-10, -20, -30, -40, -50] +``` From e38176767bc32d1bee279b884ceedb1c7521e670 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 20:25:45 +0200 Subject: [PATCH 7/7] Add when --- snippets/when.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/when.md diff --git a/snippets/when.md b/snippets/when.md new file mode 100644 index 000000000..e45fac3b1 --- /dev/null +++ b/snippets/when.md @@ -0,0 +1,19 @@ +--- +title: when +tags: function,intermediate +--- + +Tests a value, `x`, against a `predicate` function, conditionally applying a function. + +Check if the value of `predicate(x)` is `True` and if so return `when_true(x)`, otherwise return `x`. + +```py +def when(predicate, when_true): + return lambda x: when_true(x) if predicate(x) else x +``` + +```py +double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2) +double_even_numbers(2) # 4 +double_even_numbers(1) # 1 +```