combinators

parser combinator: how to terminate repetition on keyword

空扰寡人 提交于 2019-12-19 04:12:33
问题 I'm trying to figure out how to terminate a repetition of words using a keyword. An example: class CAQueryLanguage extends JavaTokenParsers { def expression = ("START" ~ words ~ "END") ^^ { x => println("expression: " + x); x } def words = rep(word) ^^ { x => println("words: " + x) x } def word = """\w+""".r } When I execute val caql = new CAQueryLanguage caql.parseAll(caql.expression, "START one two END") It prints words: List(one, two, END) , indicating the words parser has consumed the END

Parallel map in haskell

爷,独闯天下 提交于 2019-12-17 22:13:49
问题 Is there some substitute of map which evaluates the list in parallel? I don't need it to be lazy. Something like: pmap :: (a -> b) -> [a] -> [b] letting me pmap expensive_function big_list and have all my cores at 100%. 回答1: Yes, see the parallel package: ls `using` parList rdeepseq will evaluate each element of the list in parallel via the rdeepseq strategy. Note the use of parListChunk with a good chunk value might give better performance if your elements are too cheap to get a benefit

Is it possible for the presented case to be optimized into one loop?

懵懂的女人 提交于 2019-12-13 13:05:01
问题 Suppose I have two functions f :: [a] -> b and g :: [a] -> c . I have the following two questions: If I perform (f &&& g) xs where xs :: [a] , and if both f and g involve loops, is it possible for the compiler to optimize these two loops into one? (Please note that I am not asking whether some specific Haskell compiler implements this. I want to know whether such a thing is possible .) Can the traverse function from Traverse type class help me have such an optimization with something along

What is this “and” in ScalaJsonCombinator (when defining a Writes)?

梦想与她 提交于 2019-12-12 03:23:58
问题 I've been using this json combinator for several basic / standard cases without really understanding how it works. All was fine. Now I want to get myself prepared for whatever advanced cases might come; I need to understand the code. Ref.: https://www.playframework.com/documentation/2.3.x/ScalaJsonCombinators I think I can understand the Reads: implicit val locationReads: Reads[Location] = ( (JsPath \ "lat").read[Double] and (JsPath \ "long").read[Double] )(Location.apply _) It creates a

How do I execute a function only once in CoffeeScript

拥有回忆 提交于 2019-12-12 01:44:34
问题 I want to make a CoffeeScript function that even if it is invoked multiple times, has its effects only run once. Is one of these, or another way a good way to make a once-invokable function ? Is the extra do an issue or actually better ? once_maker_a = (f)-> done=false -> f.call() unless done done=true once_maker_b = (f)-> do(done=false)-> -> f.call() unless done done=true oa = once_maker_a(-> console.log 'yay A') ob = once_maker_b(-> console.log 'yay B') oa() yay A #runs the function passed

Combining two strings into one string which eliminates the same letters in C

泪湿孤枕 提交于 2019-12-11 16:46:12
问题 Hello i'm so new in the programming and want to learn some from you :) I'm doing a program in .c and i am stucked in a part. I want to get 3 or more inputs max size of 5 characters. (For example: HELLO, HI, GOOD, BYE) And i want to stack them in a new string which holds same letters only once from those 4 strings (Example: H,E,L,L,O,I,G,D,B,Y) #include <stdio.h> #include <string.h> int main(void) { char first[5], second[5], third[5], fourth[5]; printf("Enter 1st word: \n"); scanf(" %5s",

Unable to get implementation of Y combinator working

你说的曾经没有我的故事 提交于 2019-12-11 07:56:53
问题 Here's the code (also here): #lang racket (define poorY ((lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))))) When I run it: > (poorY '(9 7 8)) . . application: not a procedure; expected a procedure that can be applied to arguments given: '(#<procedure>) arguments...: '(#<procedure>) The screenshot looks like this: I'm using DrRacket as the repl. What's wrong

Haskell: Types of function composition not matching

廉价感情. 提交于 2019-12-11 01:24:08
问题 I am having some troubles with function composition and types. I would like to compose filter (which returns a list) with len , which takes a list as an argument (technically a Foldable but I am simplifying here). Looking at the types everything is as expected: > :t length length :: Foldable t => t a -> Int > :t filter filter :: (a -> Bool) -> [a] -> [a] So now I would expect the type of (len . filter) to be (length . filter) :: (a -> Bool) -> [a] -> Int while in reality is > :t (length .

define a form as function name?

江枫思渺然 提交于 2019-12-10 20:38:13
问题 I'd like to know what this code means in Scheme: (define ((K x) y) x) (define (((S x) y) z) ((x z) (y z))) The whole file is here. Is this legal Scheme? Is (K x) a parametrized function, something like generic functions in Java? I looked up the MIT Scheme reference, there seems to be nothing mentioned for definition of this kind. 回答1: Trying it in MIT Scheme works (define ((K x) y) x) ;Value: k ((k 3) 4) ;Value: 3 Apparently, these are the definitions for K and S combinators from a

Scala: Can I nudge a combinator parser to be locally greedy?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 02:48:58
问题 Suppose I have an ambiguous language expressed in combinator parser. Is there a way to make certain expressions locally greedy? Here's an example of what I mean. import scala.util.parsing.combinator._ object Example extends JavaTokenParsers { def obj: Parser[Any] = (shortchain | longchain) ~ anyrep def longchain: Parser[Any] = zero~zero~one~one def shortchain: Parser[Any] = zero~zero def anyrep: Parser[Any] = rep(any) def any: Parser[Any] = zero | one def zero: Parser[Any] = "0" def one: