functional-programming

Managing array of monads in fp-ts and Functional Programming

血红的双手。 提交于 2020-07-07 12:04:29
问题 I'm very new to Functional Programming and I'm struggling a lot with running traverse on arrays. When I read this book it seems that I should be able to simply traverse between Monads but I can't wrap my head around this concept with fp-ts. Can someone explain the following using array.traverse/sequence or any other ways please? How can I go from TaskEither<Error, string[]> to TaskEither<Error, Either<Error, string>[]> ; or is there a better way to go from a single error to nested errors

In Python, partial function application (currying) versus explicit function definition

坚强是说给别人听的谎言 提交于 2020-07-04 10:31:32
问题 In Python, is it considered better style to: explicitly define useful functions in terms of more general, possibly internal use, functions; or, use partial function application to explicitly describe function currying? I will explain my question by way of a contrived example. Suppose one writes a function, _sort_by_scoring, that takes two arguments: a scoring function and a list of items. It returns a copy of the original list sorted by scores based on each item's position within the original

How to tell when to create a new component?

僤鯓⒐⒋嵵緔 提交于 2020-07-04 08:55:07
问题 I've been looking around behind the logic of when someone shall create a new component in a web application on angularjs / angular but I suppose this is more general and might apply on all component based front end frameworks. I know that there are some principles like it should be abstract and reusable but e.g. I have seen on angular docs that each separate route looks at a specific component (how can this be reusable). Is there any solid question which I might ask before creating a new

Dart: mapping a list (list.map)

怎甘沉沦 提交于 2020-07-04 08:18:04
问题 I have a list of String s, e.g., var moviesTitles = ['Inception', 'Heat', 'Spider Man']; and wanted to use moviesTitles.map to convert them to a list of Tab Widget s in Flutter. 回答1: you can use moviesTitles.map((title) => Tab(text: title)).toList() example: bottom: new TabBar( controller: _controller, isScrollable: true, tabs: moviesTitles.map((title) => Tab(text: title)).toList() , ), 回答2: I'm new to flutter. I found that one can also achieve it this way. tabs: [ for (var title in

Intermediate values during folding?

為{幸葍}努か 提交于 2020-06-28 03:59:12
问题 I have a collection of objects case class Record(value: Whatever) val list: List[Record] and want to select the best ranked list.foldLeft(list.head) { (best, current) => if (rank(current.value) > rank(best.value)) { current } else { best } } Let's suppose that rank is expensive and better not be called twice on the same object. What are my options? I can fold to tuple (rank, record) but this probably means creating auxiliary objects during iteration. Should I worry about the overhead? Or

ELM get query parameter as string

落爺英雄遲暮 提交于 2020-06-28 03:21:37
问题 Based on this post and thanks to the @glennsl iam getting some where. First if someone has a link that i could learn about the parses i will be very glad. page : Url.Url -> String page url = case (Parser.parse (Parser.query (Query.string "name")) url) of Nothing -> "My query string: " ++ (Maybe.withDefault "empty" url.query) Just v -> case v of Just v2 -> "Finnaly a name" Nothing -> "????" As far i can understand the expression Parser.parse (Parser.query (Query.string "name")) url is

Running an array of TaskEithers in parallel, but continue if 1 or more task fails

久未见 提交于 2020-06-27 18:42:07
问题 I have to make an array of IO calls in parallel, and merge the contents of the call if successful. If one fails the others get processed as per normal, but an error message. My thought process on how this can be implemented: Array<TE<E, A>> -> TE<E, Array<A>> -> TE<E, MergedA> -> [E, A] What I'm currently doing: I am currently sequencing an array of TE, but any failure in the chain will yield a left. pipe( sequenceT(TE.taskEither)(arrayofTE), //TE<E,A>[] -> TE<E,A[]> TE.map(mergeFn), //TE<E,

Create List of Employees with dynamic values using Java streams

拟墨画扇 提交于 2020-06-27 06:31:43
问题 I have use case where I have to create List of default employees with incrementing id, List<Employee> employeeList = new ArrayList<>(); int count = 0; while (count++ <= 100){ Employee employee = new Employee(count, "a"+count); employeeList.add(employee); } I don't have any collection on which I could use stream. Can we do it in functional way? 回答1: You can use IntStream with rangeClosed(int startInclusive, int endInclusive) to generate the count List<Employee> employeeList = IntStream

Left to right application of operations on a list in python3

。_饼干妹妹 提交于 2020-06-25 09:51:12
问题 Is there any possible way to achieve a non-lazy left to right invocation of operations on a list in python ? e.g. scala val a = ((1 to 50) .map(_ * 4) .filter( _ <= 170) .filter(_.toString.length == 2) .filter (_ % 20 == 0) .zipWithIndex .map{ case(x,n) => s"Result[$n]=$x"} .mkString(" .. ")) a: String = Result[0]=20 .. Result[1]=40 .. Result[2]=60 .. Result[3]=80 While I realize many folks will not prefer the above syntax, I like the ability to move left to right and add arbitrary operations

When should one use a Kleisli?

一个人想着一个人 提交于 2020-06-24 03:24:09
问题 I recently stumbled on the concept of a Kleisli and every tutorial/link/reference that I read motivates the use of Kleisli via the following constructs: Composing functions that return monads : f: a -> m[b] with g: b -> m[c] - I think the very definition of a monad already captures this case - do/bind/for/flatMap do that. One needn't lean on the Kleisli construct to achieve this. So this cannot be the "primary" use case of a Kleisli IMO. Inserting configuration : This one states that if