traversable

How to apply a traversable of functions to a one value

放肆的年华 提交于 2021-01-28 17:49:28
问题 What should I use if I want to have something like [a->b] -> a -> [b] basically I have a list of functions, all take in a value a and returns b . I want to apply all of them to one a and get the results [b] . Which one should I use? Thanks 回答1: You don't need Traversable , just Functor : swingMap f x = fmap ($ x) f See also the swing function (this is equivalent to swing fmap ). Or, if you're using Edward Kmett's distributive library, you can have the best of both this answer (only a Functor

How to write a Traversable instance for function, in Haskell?

痴心易碎 提交于 2021-01-28 05:28:12
问题 How do I write the Traversable instance for ((->) a) ? I think I could do it, if I could generically unwrap an Applicative Functor : instance Traversable ((->) k) where -- traverse :: (a -> f b) -> (k -> a) -> f (k -> b) -- traverse h t = ? -- h :: Applicative f => a -> f b -- t :: k -> a -- h . t :: k -> f b -- unwrap . h . t :: k -> b -- pure $ unwrap . h . t :: f (k -> b) traverse h t = pure $ unwrap . h . t unwrap :: (Functor f, Applicative f) => f a -> a unwrap y@(pure x) = x But, alas,

what does the A stand for in sequenceA?

我们两清 提交于 2019-12-20 01:14:20
问题 What does sequenceA from Traversable stand for? Why is there capital A at the end? I've been learning Haskell for a few months now and this is one of those things that's been bugging me for a while. 回答1: The "A" stands for Applicative , as in the constraint in sequenceA 's type: sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) That the "A" is there is fruit of a historical accident. Once upon a time, neither Applicative nor Traversable existed in Haskell. Nonetheless, a

Why is scala.collection.immutable.List[Object] not GenTraversableOnce[?]

↘锁芯ラ 提交于 2019-12-13 04:16:19
问题 Simple question, and sorry if this is a stupid question as I am just beginning in scala. I am getting a type mismatch error that says: found : (AnyRef, org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable) => List[Object] required: ((AnyRef, org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable)) => scala.collection.GenTraversableOnce[?] But according to this post (I have a Scala List, how can I get a TraversableOnce?), a scala.collection.immutable.List is an Iterable and

Scala prevent mixing methods

不打扰是莪最后的温柔 提交于 2019-12-11 02:26:32
问题 I would like to create the following trait: trait IntSet[A] extends Traversable[A] { self: Product => def foreach[U](f: A => U): Unit } case class AProduct(a: List[Int], b: List[Int]) extends IntSet[Int] { def foreach[U](f: Int => U): Unit = { for(aa <- a; bb <- b) f(aa*bb) } } AProduct(List(1, 5,6,7), List(2,3,4,5)).toString returns (2, 3, 4, 5, 10, 15, 20, 25, 12, 18, 24, 30, 14, 21, 28, 35) But I don't want the toString method from the case class to be overriden by the one of the

How to convert Option[Try[_]] to Try[Option[_]]?

半城伤御伤魂 提交于 2019-12-09 17:24:04
问题 I quite often use the function below to convert Option[Try[_]] to Try[Option[_]] but it feels wrong. Can be such a functionality expressed in more idiomatic way? def swap[T](optTry: Option[Try[T]]): Try[Option[T]] = { optTry match { case Some(Success(t)) => Success(Some(t)) case Some(Failure(e)) => Failure(e) case None => Success(None) } } Say I have two values: val v1: Int = ??? val v2: Option[Int] = ??? I want to make an operation op (which can fail) on these values and pass that to

What would be the “distinct method” that Traversable has in addition to Foldable?

≡放荡痞女 提交于 2019-12-05 23:21:26
问题 Foldable is a superclass of Traversable, similarly to how Functor is a superclass of Applicative and Monad . Similar to the case of Monad , where it is possible to basically implement fmap as liftM :: Monad m => (a->b) -> m a -> m b liftM f q = return . f =<< q we could also emulate foldMap as foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m foldLiftT f = fst . traverse (f >>> \x -> (x,x)) -- or: . sequenceA . fmap (f >>> \x -> (x, x)) using the Monoid m => (,) m monad. So the

How to detect if object is Traversable in PHP?

荒凉一梦 提交于 2019-12-05 17:22:38
问题 How can I detect the variable is a Traversable object to use in foreach loops? if(is_traversable($variable)) { return (array) $variable; } 回答1: Use instanceof to determine if the object is Traversable if($variable instanceof \Traversable) { // is Traversable } 回答2: is_iterable can be used since PHP 7.1. // https://wiki.php.net/rfc/iterable var_dump( true === is_iterable([1, 2, 3]), true === is_iterable(new ArrayIterator([1, 2, 3])), true === is_iterable((function () { yield 1; })()) ); 来源:

How to convert Option[Try[_]] to Try[Option[_]]?

喜欢而已 提交于 2019-12-04 06:16:56
I quite often use the function below to convert Option[Try[_]] to Try[Option[_]] but it feels wrong. Can be such a functionality expressed in more idiomatic way? def swap[T](optTry: Option[Try[T]]): Try[Option[T]] = { optTry match { case Some(Success(t)) => Success(Some(t)) case Some(Failure(e)) => Failure(e) case None => Success(None) } } Say I have two values: val v1: Int = ??? val v2: Option[Int] = ??? I want to make an operation op (which can fail) on these values and pass that to function f below. def op(x: Int): Try[String] def f(x: String, y: Option[String]): Unit I typically use for

What would be the “distinct method” that Traversable has in addition to Foldable?

孤街浪徒 提交于 2019-12-04 03:50:38
Foldable is a superclass of Traversable , similarly to how Functor is a superclass of Applicative and Monad . Similar to the case of Monad , where it is possible to basically implement fmap as liftM :: Monad m => (a->b) -> m a -> m b liftM f q = return . f =<< q we could also emulate foldMap as foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m foldLiftT f = fst . traverse (f >>> \x -> (x,x)) -- or: . sequenceA . fmap (f >>> \x -> (x, x)) using the Monoid m => (,) m monad. So the combination of superclass and methods bears in both cases a certain redundancy. In case of monads, it