monoids

What's the practical value of all those newtype wrappers in `Data.Monoid`?

邮差的信 提交于 2020-01-09 19:50:48
问题 When looking at Data.Monoid , I see there are various newtype wrappers, such as All , Sum , or Product , which encode various kinds of monoids. However, when trying to use those wrappers, I can't help but wonder what's the benefit over using their non- Data.Monoid counterparts. For instance, compare the rather cumbersome summation print $ getSum $ mconcat [ Sum 33, Sum 2, Sum 55 ] vs. the more succinct idiomatic variant print $ sum [ 33, 2, 55 ] But what's the point? Is there any practical

Problems using Nothing bottom type while trying to create generic zeros for parametrized monoids

两盒软妹~` 提交于 2020-01-06 14:15:13
问题 Here's my code. It permits to create typesafe MongoDB queries using Casbah trait TypesafeQuery[ObjectType, BuildType] { def build: BuildType } trait TypesafeMongoQuery[ObjectType] extends TypesafeQuery[ObjectType, DBObject] case class TypesafeMongoQueryConjunction[ObjectType](queries: Seq[TypesafeMongoQuery[ObjectType]]) extends TypesafeMongoQuery[ObjectType] { override def build(): DBObject = $and(queries.map(_.build)) } case class TypesafeMongoQueryDisjunction[ObjectType](queries: Seq

Scala Monoid[Map[A,B]]

两盒软妹~` 提交于 2020-01-03 14:16:15
问题 I'm reading a book with the following: sealed trait Currency case object USD extends Currency ... other currency types case class Money(m: Map[Currency, BigDecimal]) { ... methods defined } The discussion goes on to recognize certain types of operations on Money as being Monoidal so we want to create a Monoid for Money . What comes next though are listings I can't parse properly. First is the definition of zeroMoney . This is done as follows: final val zeroMoney: Money = Money(Monoid[Map

How can I express foldr in terms of foldMap for type-aligned sequences?

空扰寡人 提交于 2020-01-02 01:18:25
问题 I'm playing around with type-aligned sequences, and in particular I'm messing around with the idea of folding them. A foldable type-aligned sequence looks something like this: class FoldableTA fm where foldMapTA :: Category h => (forall b c . a b c -> h b c) -> fm a b d -> h b d foldrTA :: (forall b c d . a c d -> h b c -> h b d) -> h p q -> fm a q r -> h p r foldlTA :: ... It's pretty easy to implement foldrTA in terms of foldMapTA by first using foldMapTA to convert the sequence to a type

Algorithm to multiply edges of a Networkx graph

我与影子孤独终老i 提交于 2020-01-01 20:58:11
问题 So my problem is to find the longest path from a node to another node (or the same node) in a graph implemented with Networkx library. I don't want to add the edges' weights but multiply them and take the biggest result. Obviously, passing only once by each node or not at all. For example if I want to go from node 1 to node 4, the best result would be : 2 x 14 x 34 x 58 Graph example Thank you for your help ! 回答1: This may work: import networkx as nx G = nx.Graph() # create the graph G.add

How to write monoid protocol in Clojure?

孤人 提交于 2020-01-01 04:43:15
问题 The following does not work, for obvious reasons. (defprotocol Monoid (mappend [a b]) (mzero [])) mzero has zero arguments, and zero argument methods are not allowed (or do not make sense) in protocols. In Haskell or Scala, where the dispatch is type-based rather than value-based, this is not a problem. What would be the correct way to conceptualize and write Monoid protocol in Clojure? 回答1: looking at the source, the way that this is implemented in the new reducers library is not as a

Can you formulate the insertion sort as a monoid in Clojure?

旧时模样 提交于 2020-01-01 03:37:15
问题 This is the code for an insertion sort in Clojure: (defn in-sort! [data] (letfn [(insert ([raw x](insert [] raw x)) ([sorted [y & raw] x] (if (nil? y) (conj sorted x) (if (<= x y ) (concat sorted [x,y] raw) (recur (conj sorted y) raw x )))))] (reduce insert [] data))) ;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) ;Returns: [1 2 3 4 5 6 7 8 9] This is the insertion sort formulated as a monoid in Haskell: newtype OL x = OL [x] instance Ord x => Monoid (OL x) where mempty = OL [] mappend (OL xs) (OL ys)

Endofunction as Monoid

北战南征 提交于 2019-12-30 09:56:10
问题 I'm trying this (for learning purposes): {-# LANGUAGE FlexibleInstances #-} instance Monoid (a -> a) where mempty = id mappend f g = f . g expecting id <> id to be equal to id . id However, with (id <> id) 1 I receive this error: Non type-variable argument in the constraint: Monoid (a -> a) What should I change to run it? It's just to understand monoids and Haskell typeclasses better, not for any practical usage . 回答1: This will need {-# OVERLAPPING #-} pragma since GHC.Base has an instance

Can you formulate the Bubble sort as a monoid or semigroup?

大憨熊 提交于 2019-12-30 09:32:33
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr

Can you formulate the Bubble sort as a monoid or semigroup?

一个人想着一个人 提交于 2019-12-30 09:31:48
问题 Given the following pseudocode for the bubble-sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Here is the code for Bubble Sort as Scala def bubbleSort[T](arr: Array[T])(implicit o: Ordering[T]) { import o._ val consecutiveIndices = (arr