function-composition

Function composition of methods, functions, and partially applied functions in Scala

谁说胖子不能爱 提交于 2019-12-03 10:12:50
Somewhat similar to Stack Overflow question Compose and andThen methods , I've been working through Twitter's Scala School tutorial and quickly ran into the same problem that a commenter had (which was great, because I went to bed thinking my problem was solved). In the tutorial, it defines two methods as such: def addUmm(x: String) = x + " umm" def addAhem(x: String) = x + " ahem" and while in newer versions of Scala, you can't call compose on them as such: addUmm(_).compose(addAhem(_)) , the accepted answer (and some of the other answers seem to hinge upon the fact that addUmm and addAhem

Understanding `andThen`

泪湿孤枕 提交于 2019-12-03 08:42:31
问题 I encountered andThen , but did not properly understand it. To look at it further, I read the Function1.andThen docs def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm is a MultiMap instance. scala> mm res29: scala.collection.mutable.HashMap[Int,scala.collection.mutable.Set[String]] with scala.collection.mutable.MultiMap[Int,String] = Map(2 -> Set(b) , 1 -> Set(c, a)) scala> mm.keys.toList.sortWith(_ < _).map(mm.andThen(_.toList)) res26: List[List[String]] = List(List(c, a), List(b)) scala> mm.keys

compose function and functional module

家住魔仙堡 提交于 2019-12-03 06:29:43
Python 3.2 documentation refers to Collin Winter's functional module which contains function compose : The compose() function implements function composition. In other words, it returns a wrapper around the outer and inner callables, such that the return value from inner is fed directly to outer. Unfortunately, this module hasn't been updated since July 2006; I wonder if there's any replacement available. For now, I only need compose function. Is the following original functional.compose definition still good for Python 3? def compose(func_1, func_2, unpack=False): """ compose(func_1, func_2,

Composing a Java Function and Consumer

扶醉桌前 提交于 2019-12-03 03:08:06
What is the best way to functionally compose a java Function and a Consumer ? For example given some Function<Object, String> f and some Consumer<String> c then doing f.andThen(c) would feel natural, however that is not how the interfaces work. The two options I see are either replace Consumer<String> c with Function<String, Void> c or change Consumer<String> c to BiConsumer<Function<Object, String>, String> c and do accept(Function<Object, String> f, Object o) { String thing = f.apply(o); //do something with thing } Is one of these better than the other? Is there a better way? Is this what

Is it a good idea to have a syntax sugar to function composition in Python?

狂风中的少年 提交于 2019-12-03 01:43:15
Some time ago I looked over Haskell docs and found it's functional composition operator really nice. So I've implemented this tiny decorator: from functools import partial class _compfunc(partial): def __lshift__(self, y): f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) return _compfunc(f) def __rshift__(self, y): f = lambda *args, **kwargs: y(self.func(*args, **kwargs)) return _compfunc(f) def composable(f): return _compfunc(f) @composable def f1(x): return x * 2 @composable def f2(x): return x + 3 @composable def f3(x): return (-1) * x @composable def f4(a): return a + [0] print

Python function composition

北慕城南 提交于 2019-12-03 01:13:30
I've tried to implement function composition with nice syntax and here is what I've got: from functools import partial class _compfunc(partial): def __lshift__(self, y): f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) return _compfunc(f) def __rshift__(self, y): f = lambda *args, **kwargs: y(self.func(*args, **kwargs)) return _compfunc(f) def composable(f): return _compfunc(f) @composable def f1(x): return x * 2 @composable def f2(x): return x + 3 @composable def f3(x): return (-1) * x print f1(2) #4 print f2(2) #5 print (f1 << f2 << f1)(2) #14 print (f3 >> f2)(2) #1 print (f2 >> f3)

Understanding `andThen`

 ̄綄美尐妖づ 提交于 2019-12-03 00:12:10
I encountered andThen , but did not properly understand it. To look at it further, I read the Function1.andThen docs def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm is a MultiMap instance. scala> mm res29: scala.collection.mutable.HashMap[Int,scala.collection.mutable.Set[String]] with scala.collection.mutable.MultiMap[Int,String] = Map(2 -> Set(b) , 1 -> Set(c, a)) scala> mm.keys.toList.sortWith(_ < _).map(mm.andThen(_.toList)) res26: List[List[String]] = List(List(c, a), List(b)) scala> mm.keys.toList.sortWith(_ < _).map(x => mm.apply(x).toList) res27: List[List[String]] = List(List(c, a), List(b))

Why is function composition in Haskell right associative?

♀尐吖头ヾ 提交于 2019-12-02 17:58:33
Mathematically the function composition operation is associative. Hence: f . (g . h) = (f . g) . h Thus the function composition operation may be defined to be either left associative or right associative. Since normal function application in Haskell (i.e. the juxtaposition of terms, not the $ operation) is left associative in my opinion function composition should also be left associative. After all most people in the world (including myself) are used to reading from left to right. Nevertheless function composition in Haskell is right associative: infixr 9 . I know that it doesn't really make

A function composition operator in Python

喜欢而已 提交于 2019-12-02 07:52:13
In this question I asked about a function composition operator in Python. @Philip Tzou offered the following code, which does the job. import functools class Composable: def __init__(self, func): self.func = func functools.update_wrapper(self, func) def __matmul__(self, other): return lambda *args, **kw: self.func(other.func(*args, **kw)) def __call__(self, *args, **kw): return self.func(*args, **kw) I added the following functions. def __mul__(self, other): return lambda *args, **kw: self.func(other.func(*args, **kw)) def __gt__(self, other): return lambda *args, **kw: self.func(other.func(

Partially apply several functions in Haskell

[亡魂溺海] 提交于 2019-12-02 01:08:20
问题 Suppose, in Haskell, I have a bunch of functions that all depend on the same parameter type: f :: Par -> a -> b g :: Par -> b -> c As I'm writing more of these functions that still depend on this parameter type, I can do something like h :: Par -> a -> c h par = myg . myf where myf = f par myg = g par However I keep having to write these where lines. The question is: can this be avoided? [Edit: I tried to provide a minimal example to illustrate the problem but apparently the example is too