functional-programming

“Any function on finite lists that is defined by pairing the desired result with the argument list can always be redefined in terms of fold”

懵懂的女人 提交于 2019-12-21 07:30:53
问题 I was reading through the paper A tutorial on the universality and expressiveness of fold, and am stuck on the section about generating tuples. After showing of how the normal definition of dropWhile cannot be defined in terms of fold, an example defining dropWhile using tuples was proved: dropWhile :: (a -> Bool) -> [a] -> [a] dropWhile p = fst . (dropWhilePair p) dropWhilePair :: (a -> Bool) -> [a] -> ([a], [a]) dropWhilePair p = foldr f v where f x (ys,xs) = (if p x then ys else x : xs, x

Why is the non-deterministic choice function in Curry's std lib not defined straightforwardly but rather with a helper 2-argument function?

情到浓时终转凉″ 提交于 2019-12-21 07:26:07
问题 Consider a function choose in Curry programming language with the specification that " (choose xs) non-deterministically chooses one element from the list xs ". I'd implement it straighforwardly through two alternative non-deterministic rules: choose :: [a] -> a choose x:_ = x choose _:xs = choose xs But in /usr/lib/curry-0.9.11/Success.curry from Muenster Curry Compiler, it is defined with a helper function: choose (x:xs) = choosep x xs where choosep x [] = x choosep x (_:_) = x choosep _ (x

Inverse of Supplier<T> in Guava

橙三吉。 提交于 2019-12-21 07:24:13
问题 I'm looking for the inverse of Supplier<T> in Guava. I hoped it would be called Consumer – nope – or Sink – exists, but is for primitive values. Is it hidden somewhere and I'm missing it? I'd like to see it for the same kinds of reasons that Supplier is useful. Admittedly, uses are less common, but many of the static methods of Suppliers , for example, would apply in an analogous way, and it would be useful to express in one line things like "send this supplier every value in this iterable".

How do I express this in Typescript?

回眸只為那壹抹淺笑 提交于 2019-12-21 07:10:09
问题 Let's say I have an interface A : interface A { foo: number bar: string } And I have a generic type Option : type Option<T> = { map: () => T } Then I create a new interface B from A and Option : interface B { foo: Option<number> bar: Option<string> } How can I make this operation more general? Ie. The API I want is: type B = Lift<A> Where Lift automatically maps each member of A to an Option . Note that A can have any number of members, of any type. How can I implement Lift ? If this is not

Stop for-comprehension mid-flow when using stacked monads of State and IO

狂风中的少年 提交于 2019-12-21 05:46:13
问题 In this Scala example I need to stop when the result is StopNow , I need to do this after calling decisionStep . How can I do that? case class BusinessState() trait BusinessResult case object KeepGoing extends BusinessResult case object StopNow extends BusinessResult type IOState[S, A] = StateT[IO, S, A] type BusinessIOState[A] = IOState[BusinessState, A] trait SomeSteps { def step1:BusinessIOState[Unit] def step2:BusinessIOState[BusinessState] def decisionStep:BusinessIOState[BusinessResult]

Universal Func<> type in c#

半腔热情 提交于 2019-12-21 05:31:42
问题 I'm writing a small Lisp interpreter in C#, and it's basically working already. Currently I'm using an interface to represent functions: public interface LispFunction { object Apply(ArrayList parameters); } The interface is implemented by multiple classes for internal (standard) functions, lambdas, macro expansion, calling methods in .net objects via reflection and so on. Please note that speed is NOT an issue here, just the joy of getting the interpreter to, and use it at, work. Now I'd like

What are cumulative universes and `* : *`?

坚强是说给别人听的谎言 提交于 2019-12-21 04:46:27
问题 In Agda, there is Set n . As I understand, Set n extends the Haskell-style value-type-kind hierarchy to infinite levels. That is, Set 0 is the universe of normal types, Set 1 is the universe of normal kinds, Set 2 is the universe of normal sorts, etc. In contrast, Idris has the so called "cumulative hierarchy of universes". It seems that for a < b , Type a: Type b , and universe levels are inferred. But what does it mean in real world programs? Can't we define something that only operate on

Which Javascript functional library: Underscore or wu.js or Functional or …? [closed]

守給你的承諾、 提交于 2019-12-21 04:38:27
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I am building a node.js app and wondering which javascript lib to add to my repertoire. Current short list includes: Underscore wu

Implementing List#flatMap

二次信任 提交于 2019-12-21 04:38:10
问题 Is there a better functional way to write flatMap ? def flatMap[A,B](list: List[A])(f: A => List[B]): List[B] = list.map(x => f(x)).flatten Conceptually, I understand flatMap in terms of flatten . 回答1: An alternate approach: def flatMap[A, B](list: List[A])(f: A => List[B]): List[B] = list.foldLeft(List[B]())(_ ++ f(_)) I don't know about “better”. (And if we start talking about efficient implementation, that's another can of worms...) 回答2: Just to flesh out the answers, you could also define

Scala — How to use Functors on non-Function types?

回眸只為那壹抹淺笑 提交于 2019-12-21 04:37:12
问题 While reading the description of Functors on this blog: https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/ there is a generic definition of Functor and a more specific one: trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] { def fmap[A, B](f: A ->> B): F[A] ->>> F[B] } trait Functor[F[_]] extends GenericFunctor[Function, Function, F] { final def fmap[A, B](as: F[A])(f: A => B): F[B] = fmap(f)(as) } Clearly this means Functors can be used with other higher