functor

How to simplify nested map calls?

老子叫甜甜 提交于 2019-12-01 03:41:39
Suppose I have a few nested functors, e.g. List[Option[Int]] and need to call the map of the most inner one. Now I am using nested maps : scala> val opts: List[Option[Int]] = List(Some(0), Some(1)) opts: List[Option[Int]] = List(Some(0), Some(1)) scala> opts.map(o => o.map(_ + 1)) res0: List[Option[Int]] = List(Some(1), Some(2)) What if I have 3 nesting levels, for instance ? Is there any simple alternative to nested maps ? Yes, this is possible with scalaz.Functor: scala> import scalaz.Functor import scalaz.Functor scala> import scalaz.std.list._ import scalaz.std.list._ scala> import scalaz

C++ functor as a function pointer

♀尐吖头ヾ 提交于 2019-12-01 03:24:39
问题 I have a Functor which I need to send to a function which receives a function pointer as a parameter (such as CreateThread ). Can I convert it to a static method address somehow? And if not, how can I do it? 回答1: No, you can't convert a class-type object to a function pointer. However, you can write a non-member wrapper function that calls the member function on the right instance. Many APIs, including CreateThread , allow you to provide a pointer that it will give back to you when it calls

Monad more powerful than Applicative?

独自空忆成欢 提交于 2019-12-01 02:02:18
I looked at past discussion but could not see why any of the answers are actually correct. Applicative <*> :: f (a -> b) -> f a -> f b Monad (>>=) :: m a -> (a -> m b) -> m b So if I get it right, the claim is that >>= cannot be written by only assuming the existence of <*> Well, let's assume I have <*> . And I want to create >>= . So I have f a . I have f (a -> b) . Now when you look at it, f (a -> b) can be written as (a -> b) (if something is a function of x, y , z - then it's also a function of x, y). So from the existence of <*> we get (a -> b) -> f a -> f b which again can be written as

How to simplify nested map calls?

会有一股神秘感。 提交于 2019-11-30 23:27:16
问题 Suppose I have a few nested functors, e.g. List[Option[Int]] and need to call the map of the most inner one. Now I am using nested maps : scala> val opts: List[Option[Int]] = List(Some(0), Some(1)) opts: List[Option[Int]] = List(Some(0), Some(1)) scala> opts.map(o => o.map(_ + 1)) res0: List[Option[Int]] = List(Some(1), Some(2)) What if I have 3 nesting levels, for instance ? Is there any simple alternative to nested maps ? 回答1: Yes, this is possible with scalaz.Functor: scala> import scalaz

OCaml functors :: counter-intuitive behaviour

大城市里の小女人 提交于 2019-11-30 22:39:45
问题 I am experimenting with the module language of OCaml (3.12.1), defining functors and signatures for modules and so on, mostly following the examples from Chapter 2 of the OCaml manual and I've stumbled, by accident, on a situation where apparently my mental model of how functors and module signatures work is flawed. I tried to narrow the situation I encountered to the shortest amount of code possible so don't ask what I am trying to accomplish, this is a totally contrived example to

When are stateless class functors useful in place of a c style function?

时光毁灭记忆、已成空白 提交于 2019-11-30 20:47:13
I've found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator() . I came across an example in a book that defines the function call operator without having state, and I can't help but feel like this is an awkward usage, and that a normal style function pointer, would be better than using operator() in every way here - less code, less variables (you have to instantiate the comparators), its probably more efficient due to the instantiation, and no loss of meaning or encapsulation (since it's just one function).

Use of a functor on for_each

妖精的绣舞 提交于 2019-11-30 19:30:15
Why does the for_each call on functor doesn't update sum::total at the end? struct sum { sum():total(0){}; int total; void operator()(int element) { total+=element; } }; int main() { sum s; int arr[] = {0, 1, 2, 3, 4, 5}; std::for_each(arr, arr+6, s); cout << s.total << endl; // prints total = 0; } for_each takes the functor by value - so it is copied. You can e.g. use a functor which is initialized with a pointer to an external int. struct sum { sum(int * t):total(t){}; int * total; void operator()(int element) { *total+=element; } }; int main() { int total = 0; sum s(&total); int arr[] = {0,

Visual Studio 2010 and std::function

落花浮王杯 提交于 2019-11-30 18:04:50
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i) const { std::cout << "F: " << i << std::endl; return i + 1; } }; int main() { A a; std::tr1::function<int(int)> f = std::tr1::ref(a); std::cout << f(6) << std::endl; } The aim is to pass the functor object by a reference_wrapper, in a way to avoid useless copy costructor calls. I expect the following output: F: 6 7 It works correctly with GCC >= 4.4.0, Visual Studio 2008 and with boost by substituting std::tr1 namespace with boost. It only doesn't work with the new Visual Studio 2010 both Express Beta

What exactly does “deriving Functor” do?

纵饮孤独 提交于 2019-11-30 17:20:31
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? 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 Functor instance. It'll then derive the "obvious" Functor instance. There's been some concern in the past that

Is there a Functor that cannot be a law-abiding Apply?

做~自己de王妃 提交于 2019-11-30 17:06:06
A recent question asked generally about boundaries between various Haskell classes. I came up with Handler as an example of a valid Functor with no sensible instance of Apply **, where class Functor f => Apply f where (<.>) :: f (a -> b) -> f a -> f b -- optional bits omitted. However, I've not yet been able to find an example of a valid Functor that cannot be made a valid (if senseless) instance of Apply . The fact that Apply has had (see update) but a single law, (.) <$> u <.> v <.> w = u <.> (v <.> w) seems to make this rather tricky. pigworker (Conor McBride) previously gave an example of