functor

why a js map on an array modify the original array?

陌路散爱 提交于 2019-11-29 20:56:41
I'm quite confuse by the behaviour of map(). I have an array of objects like this : const products = [{ ..., 'productType' = 'premium', ... }, ...] and i'm passing this array to a function that should return the same array but with all product made free : [{ ..., 'productType' = 'free', ... }, ...] The function is : const freeProduct = function(products){ return products.map(x => x.productType = "free") } Which returns the following array : ["free", "free", ...] So i rewrote my function to be : const freeProduct = function(products){ return products.map(x => {x.productType = "free"; return x})

Let Haskell functors sink in.

╄→гoц情女王★ 提交于 2019-11-29 20:08:16
Learn You a Haskell has an example about functors. I can read LYAH, and text, and figure out what is supposed to happen -- but I don't know enough to write something like this. I'm finding this problem often in Haskell. instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x However, I'm confused.. Why doesn't this comple instance Functor (Either a) where fmap f (Right x) = Right (x) fmap f (Left x) = Left (f x) If f isn't being used in the top definition, then what else constrains x such that it can't satisfy Left Here's the functor class: class Functor f

Why should I use applicative functors in functional programming?

你说的曾经没有我的故事 提交于 2019-11-29 19:55:54
I'm new to Haskell, and I'm reading about functors and applicative functors. Ok, I understand functors and how I can use them, but I don't understand why applicative functors are useful and how I can use them in Haskell. Can you explain to me with a simple example why I need applicative functors? huitseeker Applicative functors are a construction that provides the midpoint between functors and monads , and are therefore more widespread than monads, while more useful than functors. Normally you can just map a function over a functor. Applicative functors allow you to take a "normal" function

c++ functor and function templates

南笙酒味 提交于 2019-11-29 17:03:46
问题 consider this simple and pointless code. #include <iostream> struct A { template<int N> void test() { std::cout << N << std::endl; } }; int main() { A a; a.test<1>(); } It is a very simple example of a function template. What if however, I wanted to replace A::test with an overloaded operator() to make it a functor? #include <iostream> struct A { template<int N> void operator()() { std::cout << N << std::endl; } }; int main() { A a; a<1>(); // <-- error, how do I do this? } Certainly if the

What exactly does “deriving Functor” do?

我的未来我决定 提交于 2019-11-29 16:25:25
问题 I'm trying to figure out what exactly are the rules for deriving Functor in Haskell. I've seen message postings about it, and I've seen test code about it, but I can't seem to find official documentation about what the rules are. Can someone please clarify and/or point me to the right place? 回答1: To use deriving Functor you must enable the DeriveFunctor language pragma and apply it to a polymorphic type which has a covariant final type variable---in other words, a type which admits a valid

Why is 'X x; x();' allowed, when 'X' defines a conversion to function pointer, but not, when it defines a conversion to a functor?

余生颓废 提交于 2019-11-29 16:02:01
问题 void f(int){} typedef void (*f_ptr)(int); struct Functor{ void operator()(int){} }; struct X{ operator f_ptr(){ return f; } }; struct Y{ operator Functor(){ return Functor(); } }; int main(){ X x; Y y; x(5); // works ?! y(5); // doesn't ?! } Live example on Ideone. Output: error: no match for call to '(Y) (int)' Q1: Why is the call to x(5) allowed, even though X only defines a conversion to function pointer, and not operator() ? Q2: Conversely, why is the same thing not allowed, if we define

Difference in capability between fmap and bind?

佐手、 提交于 2019-11-29 12:18:11
问题 I'm new to functional programming (coming from javascript), and I'm having a hard time telling the difference between the two, which is also messing with my understand of functors vs. monads. Functor: class Functor f where fmap :: (a -> b) -> f a -> f b Monad (simplified): class Monad m where (>>=) :: m a -> (a -> m b) -> m b fmap takes a function and a functor, and returns a functor. >>= takes a function and a monad, and returns a monad. The difference between the two is in the function

std::function -> function pointer

≡放荡痞女 提交于 2019-11-29 09:47:54
Here is a code: #include <functional> using namespace std::tr1; typedef void(*fp)(void); void foo(void) { } void f(fp) { } int main() { function<void(void)> fun = foo; f(fun); // error f(foo); // ok } Originally i need to make a function pointer from non-static class method because i need to save data between function callings. I tried std::tr1::bind and boost::bind , but they return functional object, not pointer, which, as i can see, can't be "casted" to pure functional pointer. While the function signature ( SetupIterateCabinet ) demands a pure func pointer exactly. I need an advise how to

“can't existentially abstract over parameterized type…”

£可爱£侵袭症+ 提交于 2019-11-29 09:24:12
I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.as[List] So here's what I tried to do: object Test { abstract class NatTrans[F[_], G[_]] { def convert[T](f:F[T]):G[T] } implicit def array2List:NatTrans[Array, List] = new NatTrans[Array, List] { def convert[T](a:Array[T]) = a.toList } // this next part gets

How to write code in F# for what functors do in OCaml?

我的梦境 提交于 2019-11-29 06:11:16
I have many programs written in OCaml, some of them use functors. Now, I am considering of writing and re-writing a part of code in F# (to benefit some advantages that OCaml does not have). One thing I am afraid of is to write code in F# for what functors do in OCaml. For instance, how could we emulate this example from OCaml manual in F#? type comparison = Less | Equal | Greater module type ORDERED_TYPE = sig type t val compare: t -> t -> comparison end module Set = functor (Elt: ORDERED_TYPE) -> struct type element = Elt.t type set = element list let empty = [] let rec add x s = match s with