lifting

Is there a name for this kind of lifting a function?

跟風遠走 提交于 2019-12-10 18:09:33
问题 I wrote a Scala function: def liftOrIdentity[T](f: (T, T) => T) = (a: Option[T], b: Option[T]) => (a, b) match { case (Some(a), None) => Some(a) case (None, Some(b)) => Some(b) case (Some(a), Some(b)) => Some(f(a, b)) case (None, None) => None } Is there a name for this pattern? It is not quite an applicative functor due to cases 1 and 2. Feel free to answer with Haskell or Scala code. 回答1: On collection it is flatten + reduce : List(a, b).flatten.reduceOption(f) a ++ b reduceOption f // same

Difference between lifting and higher order functions

孤人 提交于 2019-12-06 08:25:09
I usually hear the term lifting, when people are talking about map , fold , or bind , but isn't basically every higher order function some kind of lifting? Why can't filter be a lift from a -> Bool to [a] -> [a] , heck even the bool function (which models an if statement) can be considered a lift from a -> a to Bool -> a . And if they are not, then why is ap from the Applicative type class considered a lift? If the important thing is going from ... a ... to ... f a ... , then ap wouldn't fit the case either: f (a -> b) -> f a -> f b I'm surprised no one has answered this already. A lifting

Lift to fix the *inside* of a monad transformer stack

吃可爱长大的小学妹 提交于 2019-12-03 21:34:17
问题 Suppose I have an IO Int wrapped in a StateT MyState , then I have a value of State MyState Int which I want to use in the stacked monad. How do I lift it in this inner sense? I already know to use lift or liftIO if I get something compatible with the inside that I just need to lift to the outside monad, but now I have the opposite problem: the value is already in the outer monad but not the inner one. For instance: checkSame :: State MyState a -> IO a -> StateT MyState IO Bool checkSame sim

Tidying up Monads - turning application of a monad transformer into newtype monad

末鹿安然 提交于 2019-11-30 19:23:47
I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by simple type synonyms: type MyState = StateT A M type MyBranch a = ExceptT a MyState then I thought

Tidying up Monads - turning application of a monad transformer into newtype monad

本秂侑毒 提交于 2019-11-30 03:07:00
问题 I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by

Can't wrap my head around “lift” in Ramda.js

孤街醉人 提交于 2019-11-28 19:20:33
Looking at the source for Ramda.js, specifically at the "lift" function. lift liftN Here's the given example: var madd3 = R.lift(R.curry((a, b, c) => a + b + c)); madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7] So the first number of the result is easy, a , b , and c , are all the first elements of each array. The second one isn't as easy for me to understand. Are the arguments the second value of each array (2, 2, undefined) or is it the second value of the first array and the first values of the second and third array? Even disregarding the order of what's happening here, I

How do I break down a chain of member access expressions?

廉价感情. 提交于 2019-11-28 17:04:42
The Short Version (TL;DR): Suppose I have an expression that's just a chain of member access operators: Expression<Func<Tx, Tbaz>> e = x => x.foo.bar.baz; You can think of this expression as a composition of sub-expressions, each comprising one member-access operation: Expression<Func<Tx, Tfoo>> e1 = (Tx x) => x.foo; Expression<Func<Tfoo, Tbar>> e2 = (Tfoo foo) => foo.bar; Expression<Func<Tbar, Tbaz>> e3 = (Tbar bar) => bar.baz; What I want to do is break e down into these component sub-expressions so I can work with them individually. The Even Shorter Version: If I have the expression x => x

What is “lifting” in Scala?

核能气质少年 提交于 2019-11-28 14:54:43
Sometimes when I read articles in the Scala ecosystem I read the term "lifting" / "lifted". Unfortunately, it is not explained what that exactly means. I did some research, and it seems that lifting has something to do with functional values or something like that, but I was not able to find a text that explains what lifting actually is about in a beginner friendly way. There is additional confusion through the Lift framework which has lifting in its name, but it doesn't help answer the question. What is "lifting" in Scala? There are a few usages: PartialFunction Remember a PartialFunction[A,

How do I break down a chain of member access expressions?

做~自己de王妃 提交于 2019-11-27 10:23:26
问题 The Short Version (TL;DR): Suppose I have an expression that's just a chain of member access operators: Expression<Func<Tx, Tbaz>> e = x => x.foo.bar.baz; You can think of this expression as a composition of sub-expressions, each comprising one member-access operation: Expression<Func<Tx, Tfoo>> e1 = (Tx x) => x.foo; Expression<Func<Tfoo, Tbar>> e2 = (Tfoo foo) => foo.bar; Expression<Func<Tbar, Tbaz>> e3 = (Tbar bar) => bar.baz; What I want to do is break e down into these component sub

What is “lifting” in Scala?

徘徊边缘 提交于 2019-11-27 08:54:40
问题 Sometimes when I read articles in the Scala ecosystem I read the term "lifting" / "lifted". Unfortunately, it is not explained what that exactly means. I did some research, and it seems that lifting has something to do with functional values or something like that, but I was not able to find a text that explains what lifting actually is about in a beginner friendly way. There is additional confusion through the Lift framework which has lifting in its name, but it doesn't help answer the