functional-programming

Elm JSON decoder for Union type with data

▼魔方 西西 提交于 2019-12-12 23:40:26
问题 My json looks like this: {"name": "providerWithVal", "value": "example"} or like this: {"name": "provider2"} or {"name": "provider3"} My Elm union type is defined like so: type Provider = ProviderWithVal String | Provider2 | Provider3 I can write a decoder for a union type without data attached. But ProviderWithVal takes a string and I'm not sure how to make it all work. This is what I have so far: import Json.Decode as D providerDecoder : D.Decoder Provider providerDecoder = D.field "name" D

How do you detect key presses on a Racket web application?

荒凉一梦 提交于 2019-12-12 21:02:28
问题 I've been through the documentation for web-servers and can't find anything on it. Here's my code for a basic web application: #lang racket (require web-server/servlet web-server/servlet-env) (define test '()) (define (start request) (define bindings (request-bindings request)) (cond ((exists-binding? `cb1 bindings) (set! test '(1 2 3)) (printf "~a" "(test) has been set to '(1 2 3)!"))) (response/xexpr `(html (head (title "My Blog")) (body (h1 "Under construction") (form ,`(input ((name "cb1"

Java: state sharing between threads in functional programming

梦想的初衷 提交于 2019-12-12 20:24:08
问题 My question is a more specific instantiation of this question: Functional programming: state vs. reassignment I'm a newbee to FP and trying to understand it through Java. I have the following class whose object is shared between multiple threads: public class Bank { private double[] accounts = new double[1000]; public synchronized void transfer(int from, int to, double amount) { account[from] -= amount; account[to] += amount; } } (This is a very simplified example, hence other details such as

Take top X total items in a round robin from multiple arrays with Ramda

拜拜、爱过 提交于 2019-12-12 19:24:42
问题 I have an array of arrays and want to write a function that returns the top x number of items, by taking items from each array in order. Here is an example of what I'm after: const input = [ ["1a", "2a", "3a", "4a", "5a"], ["1b", "2b", "3b", "4b", "5b"], ["1c", "2c", "3c", "4c", "5c"], ["1d", "2d", "3d", "4d", "5d"] ]; const takeRoundRobin = count => arr => { // implementation here }; const actual = takeRoundRobin(5)(input); const expected = [ "1a", "1b", "1c", "1d", "2a" ]; I saw a

Optionally pass parameter to function being passed around

夙愿已清 提交于 2019-12-12 19:05:15
问题 I have the following function: export function dateFormatter(date: string) { return moment(date).format("MM/DD/YYYY"); } I have a React component where I'm able to pass the function like this: <TableColumn field="endDate" format={dateFormatter}>End Date</TableColumn> That component calls the function like this: const { format, field } = column.props; if (format) { return format(cell); } return cell; All of this works fine. I would now like to alter my dateFormatter function so that I can

Haskell: function composition with anonymous/lambda function

感情迁移 提交于 2019-12-12 18:40:17
问题 While studying for a Functional Programming exam, I came across the following question from a previous test: t1 = (reverse . take 2 . words . \ _ -> name)"!" The task is to write the output of the statement. The variable name refers to the student's name, written in the form "Smith, John". If I enter the statement into WinHugs, I get the following output: ["John","Smith,"] I understand what the functions reverse, take and words are doing and I understand how the . operator connects them. What

How clojure map and keyword could be a function [duplicate]

本小妞迷上赌 提交于 2019-12-12 16:45:47
问题 This question already has answers here : Why does using keywords or symbols as functions to lookup values from maps work? (3 answers) Closed 2 years ago . In clojure, you can use map data structure or the keyword as a function to look up the map. (let [m {:foo "bar"}] (get m :foo) ; -> "bar" (:foo m) ; -> "bar" (m :foo)) ; -> "bar" All of these expression returns the value "bar". Implementation wise, how is it possible to use map or the keyword as a function? Is this a special case built into

In Scala, why can I use `Unit` as return type here?

懵懂的女人 提交于 2019-12-12 16:17:06
问题 The codes are below: scala> def f(x:Int => Unit):Unit = 1 <console>:7: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses def f(x:Int => Unit):Unit = 1 ^ f: (x: Int => Unit)Unit scala> f(_=>2); <console>:9: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses f(_=>2); ^ scala> f(_=>List(1,2)); All three expressions above worked in REPL(with some warnings), but they look a bit confusing..

“foop”: a naming convention? It's a helper recursive function for “foo”; what does the suffix “p” mean?

北城以北 提交于 2019-12-12 16:02:54
问题 I've come across the following code snippet (a function definition): choose (x:xs) = choosep x xs where choosep x [] = x choosep x (_:_) = x choosep _ (x:xs) = choosep x xs in Curry programming language in a "standard library"--/usr/lib/curry-0.9.11/Success.curry from Muenster Curry Compiler. Here: choose :: [a] -> a and choosep :: a -> [a] -> a -- BTW, not a _p_redicate Is the "p" suffix for the helper recursive function choosep a known naming convention? Perhaps it comes from functional

Combining multiple arrays into one, indexing sequentially

不羁的心 提交于 2019-12-12 14:37:42
问题 I have multiple array (max 15), each can max have 1800 objects. I need to combine them into a single array, so that I then can apply a delimiter (',') to generate a csv file. The problem is when I combine them into a single array, it has to be sequentially combined, like first objects of each array should be inserted initially followed by the 2nd index of objects then 3rd and so on. I'm able to achieve the result I want by using a for-in loop. However this doesn't very swifty. I feel it can