functor

How to show that a monad is a functor and an applicative functor?

别等时光非礼了梦想. 提交于 2019-12-03 23:30:35
Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind , how to: derive fmap , derive <*> ? ehird Well, fmap is just (a -> b) -> f a -> f b , i.e. we want to transform the monadic action's result with a pure function. That's easy to write with do notation: fmap f m = do a <- m return (f a) or, written "raw": fmap f m = m >>= \a -> return (f a) This is available as Control.Monad.liftM . pure :: a -> f a is of course return . (<*>) :: f (a -

Is it possible to create function-local closures pre-C++11?

烈酒焚心 提交于 2019-12-03 16:46:59
With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we actually need them, not somewhere where they don't really belong. In C++98/03, a nice way to make function-local functors/closures would've been the following: struct{ void operator()(int& item){ ++item; } }foo_functor; some_templated_func(some_args, foo_functor); Sadly, you can't use local types for templates (Visual Studio allows this with language extensions enabled). My train of though then went the following way: struct X{ static void functor(int& item){ ++item; } }; some_templated

What is a good data structure to represent an undirected graph?

旧时模样 提交于 2019-12-03 15:49:15
I need to construct an undirected graph. I don't need it to do anything too fancy, but ideally it would work like this: structure UDG = UndirectedGraph val g = UDG.empty val g = UDG.addEdges(g, n1, [n2, n4, n7]) (* n1 is connected to n2, n4, and n7 *) val g = UDG.addEdge(g, n2, n3) UDG.connected(g, n2) (* returns [n1, n3] *) Is there a good data structure in SML/NJ to model these relationships? Should I just roll my own? Updates I've gone ahead and tried rolling my own, but I get a type mismatch error when I try to test it. My experience with SML structures and functors is pretty basic, so I

Best Java Functor lib: JGA, commons functor, mango, or…?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 13:08:09
问题 I am interested in using functors (function objects) in Java. With quick googling I found these 3 packages: Java Generics Algorithms: http://jga.sourceforge.net/ Commons functor: http://commons.apache.org/sandbox/functor/ Mango: http://www.jezuk.co.uk/cgi-bin/view/mango and of 3, JGA seemed like it might have the best design. But I suspect others here who have actually used one or more of the packages might be able to offer more insight as to pros and cons of these (and perhaps other) functor

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

感情迁移 提交于 2019-12-03 12:49:14
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-kinded types besides Function objects. Could someone please give an example or explain how or why or in

Indexing into containers: the mathematical underpinnings

元气小坏坏 提交于 2019-12-03 12:02:43
问题 When you want to pull an element out of a data structure, you have to give its index. But the meaning of index depends on the data structure itself. class Indexed f where type Ix f (!) :: f a -> Ix f -> Maybe a -- indices can be out of bounds For example... Elements in a list have numeric positions. data Nat = Z | S Nat instance Indexed [] where type Ix [] = Nat [] ! _ = Nothing (x:_) ! Z = Just x (_:xs) ! (S n) = xs ! n Elements in a binary tree are identified by a sequence of directions.

In C++ what does it mean for a compiler to “inline” a function object?

随声附和 提交于 2019-12-03 11:36:15
问题 In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them. I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help! 回答1: The last parameter of for_each template is a functor . Functor is something that can be "called" using the () operator (possibly with arguments). By defintion, there are two distinctive kinds of functors:

Would you please explain OCaml functors to me? [duplicate]

放肆的年华 提交于 2019-12-03 11:22:13
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: In Functional Programming, what is a functor? I don't know much about OCaml, I've studied F# for some time and quite understand it. They say that F# misses functor model, which is present in OCaml. I've tried to figure out what exactly functor is, but wikipedia and tutorials didn't help me much. Could you please illuminate that mystery for me? Thanks in advance :) EDIT: I've caught the point, thx to everyone who

Why do several of the standard operators not have standard functors?

微笑、不失礼 提交于 2019-12-03 10:54:58
问题 We have: std::plus ( + ) std::minus ( - ) std::multiplies ( * ) std::divides ( / ) std::modulus ( % ) std::negate ( - ) std::logical_or ( || ) std::logical_not ( ! ) std::logical_and ( && ) std::equal_to ( == ) std::not_equal_to ( != ) std::less ( < ) std::greater ( > ) std::less_equal ( <= ) std::greater_equal ( >= ) We don't have functors for: & (address-of) * (dereference) [] , bitwise operators ~ , & , | , ^ , << , >> ++ (prefix/postfix) / -- (prefix/postfix) sizeof static_cast / dynamic

SIMD or not SIMD - cross platform

萝らか妹 提交于 2019-12-03 10:12:23
问题 I need some idea how to write a C++ cross platform implementation of a few parallelizable problems in a way so I can take advantage of SIMD (SSE, SPU, etc) if available. As well as I want to be able at run time to switch between SIMD and not SIMD. How would you suggest me to approach this problem? (Of course I don't want to implement the problem multiple times for all possible options) I can see how this might not be very easy task with C++ but I believe that I'm missing something. So far my