functional-programming

Making a datatype an instance of Functor to map on a field which is of parametric type

落花浮王杯 提交于 2020-05-16 04:07:51
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

允我心安 提交于 2020-05-16 04:07:13
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

守給你的承諾、 提交于 2020-05-16 04:06:12
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

How would I get the minVal without a loop and using only foldLeft?

倖福魔咒の 提交于 2020-05-15 07:42:24
问题 So, I have gotten a bit of an understanding of functional programming but for this I cannot use a loop and instead have to use the foldLeft or foldRight function that I wrote, on order to get the minVal. static <U,V> V foldLeft(V e, Iterable<U>l, BiFunction<V,U,V> f){ for(U u:l) { e = f.apply(e, u); } return e; } static <U,V> V foldRight(V e, Iterable<U>l, BiFunction<U,V,V> f){ for(U u:l) { e = f.apply(u, e); } return e; I now have to write a minVal: //(5) Use minVal to calculate the minimum

How to sum number of Ints and Number of Floats within a List - Scala

笑着哭i 提交于 2020-05-15 07:40:30
问题 I need to calculate the number of integers and floats i have in a Map which is like Map[String, List[(Int, String, Float)]] The data comes from reading a file - the data inside for example looks kinda like (however there is a few more Routes): Cycle Route (City),1:City Centre :0.75f,2:Main Park :3.8f,3:Central Station:2.7f,4:Modern Art Museum,5:Garden Centre:2.4f,6:Music Centre:3.4f The map is split so that the String is the name of the route and the List is the rest of the data. I want it to

Convert type pattern maching to type class

无人久伴 提交于 2020-05-15 02:28:26
问题 I have the following code: val byteBuffer = array(0) match { case _: Int => ByteBuffer.allocate(4 * array.length) case _: Long => ByteBuffer.allocate(8 * array.length) case _: Float => ByteBuffer.allocate(4 * array.length) case _: Double => ByteBuffer.allocate(8 * array.length) case _: Boolean => ByteBuffer.allocate(1 * array.length) } How can I convert it to use type class? Edit: I was asked what the type of the array is. It's complicated. The array is declared like that: val array = obj

Is implementing this words function possible without a postprocessing step after folding?

末鹿安然 提交于 2020-05-12 11:19:10
问题 Real World Haskell, chapter 4, page 98 of the print asks if words can be implemented using folds, and this is my question too: Is it possible? If not, why? If it is, how? I came up with the following, which is based on the idea that each non-space should be prepended to the last word in the output list (this happens in the otherwise guard), and that a space should trigger the appending of an emtpy word to the output list if there is not one already (this is handled in the if - then - else ).

How do I center an item within a React Native ListView?

痴心易碎 提交于 2020-05-10 21:34:40
问题 I'm trying to center an item within a horizontal listView when selected. My current strategy is to first measure the item and scroll to the x coordinates of the referenced item within the view. Currently, any time I press an item ListView scrolls to the very end x: 538 Is there an easier way to implement this while keeping the code stateless / functional? const ItemScroll = (props) => { const createItem = (obj, rowID) => { const isCurrentlySelected = obj.id === props.currentSelectedID

How do I center an item within a React Native ListView?

浪尽此生 提交于 2020-05-10 21:32:27
问题 I'm trying to center an item within a horizontal listView when selected. My current strategy is to first measure the item and scroll to the x coordinates of the referenced item within the view. Currently, any time I press an item ListView scrolls to the very end x: 538 Is there an easier way to implement this while keeping the code stateless / functional? const ItemScroll = (props) => { const createItem = (obj, rowID) => { const isCurrentlySelected = obj.id === props.currentSelectedID

When should I use function currying in Python?

∥☆過路亽.° 提交于 2020-05-10 11:57:36
问题 When should I write my functions in curried form? does not match my thought, need to correct myself. As part of my learning link, this is what I understand from function currying. Below is one example: def curry2(f): """Returns a function g such that g(x)(y) == f(x, y) >>> from operator import add >>> add_three = curry2(add)(3) >>> add_three(4) """ def g(x): def h(y): return f(x, y) return h return g In any application, if I know that the number of arguments are fixed (say 2 arguments) and