functional-programming

call a function in python script then check if condition

假如想象 提交于 2019-12-24 22:06:44
问题 I have this function: def ContentFunc(): storage = StringIO() c = pycurl.Curl() c.setopt(c.URL, url) c.setopt(c.WRITEFUNCTION, storage.write) c.perform() c.close() content = storage.getvalue() while True: ContentFunc() if "word" in content: out = open('/tmp/test', 'a+') I want to append content from content = storage.getvalue() . But doesn't work. The ERROR: NameError: name 'content' is not defined Can you help me? 回答1: In your function def ContentFunc(): ... content = storage.getvalue() This

Traversing Either in Scala

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 19:46:33
问题 I wrote the following simple code: import cats.effect.IO import cats.instances.either._ import cats.syntax.TraverseSyntax object Test extends App with TraverseSyntax{ val e: Either[String, IO[Int]] = Right(IO(2)) e.sequence //error here } Unfortunately it refuses to compile with the Error:(25, 94) value sequence is not a member of scala.util.Either Can you please explain why? I imported either instances which include Traverse[Either[A, ?]] . What's wrong? 回答1: Traverse[F] is defined as a

How to prove that a string is in hexadecimal format?

删除回忆录丶 提交于 2019-12-24 18:16:53
问题 How can I use Idris to build a function that, given a string, returns a proof that such String is hexadecimal (i.e., 0x followed by 2*N characters from 0-9 and a-f , such as "0x1a7f33b8" )? What I tried First, I've constructed the following type for hex chars: data IsNib : Char -> Type where IsNib0 : IsNib '0' IsNib1 : IsNib '1' IsNib2 : IsNib '2' IsNib3 : IsNib '3' IsNib4 : IsNib '4' IsNib5 : IsNib '5' IsNib6 : IsNib '6' IsNib7 : IsNib '7' IsNib8 : IsNib '8' IsNib9 : IsNib '9' IsNibA : IsNib

Calculate mathematical operation stored in String using regex in Scala

天大地大妈咪最大 提交于 2019-12-24 17:19:59
问题 I want to evaluate mathematical expression stored in String and print the result. I have to use Pattern matching in Scala. I wrote this code below, but it does not work, it prints false instead of 2 . Any help will be appreciated. object PatternMatcher{ val s = "13 - 5 - 6" val Pattern = "((\\d+\\s[+-]\\s){1,10}(\\d+){0,1})".r def main(args: Array[String]) { println(matcher(s)) } def matcher(choice: String): Any = choice match { case Pattern(choice) => choice case _ => "false" } } 回答1: If you

Scheme/Racket: most idiomatic way to append single element to end of list

浪尽此生 提交于 2019-12-24 16:22:23
问题 I want to append the element b to the list a (let's say (a1, a2, ... an) ), e.g. appending the number 3 to (1 2) gives (1 2 3) So far I've been doing (append a (list b)) , which is kind of long and inelegant, so I wonder if there's a "better" way... 回答1: Are you building a list piecemeal, an item at a time? If so, the idiomatic way to do this is to build the list backward, using cons , and then reversing the final result: (define (map-using-cons-and-reverse f lst) (let loop ((result '())

When a language qualifies as a functional language? [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-24 16:13:45
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . What are the traits a language should have to be qualified as a functional language? When we can say that a language XYZ supports functional paradigm? 回答1: What are the traits a language should have to be qualified as a functional language? When we can say that a language XYZ

Scala error “value map is not a member of Double”

坚强是说给别人听的谎言 提交于 2019-12-24 16:00:18
问题 Explanation: I accepted gzm0's answer because it rocked! @Eduardo did come in with a comment suggesting: (for(i <- 10..20; j=runTest(i)) yield i -> j).toMap which also lets me run build, he just never posted an answer and @gzm0 answer was conceptually AWESOME so I accepted it. Once I get this other issue figured out relating to "can't call constructor" I will be able to test these out by actually running the program LOL Question: I have an error in this expression, specifically how to fix it

How do convert this code and my thinking to a functional mindset (Clojure)?

浪尽此生 提交于 2019-12-24 14:28:22
问题 How do I convert this JavaScript code to Clojure? I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows then columns but my next step is to move the fill property around (up, down, left, right). So, I don't want an answer which wouldn't work if I were not printing the data structure. My goal is to understand how to think about this problem in a functional way. It was easy for me to solve this problem

Functional way to get previous element during map

情到浓时终转凉″ 提交于 2019-12-24 11:19:40
问题 I have an array which I map over. I need to compare the current element with the previous. I am detecting if the current element is the same as the previous element by comparing their id s and doing something different based on this condition. Is there any purely functional way to do it without doing index math? items.map((item, index) => { if(item.id === items[index - 1 > 0 ? index - 1 : 0].id) { // do something } else { // do something else } }) The code works but I would like to avoid

Scala lambda function with map function

被刻印的时光 ゝ 提交于 2019-12-24 11:05:34
问题 I define the following variable x val x = Array((3,2), (4,5)) Its type is Array[(Int, Int)] When I do the following: x.map((a: Int, b: Int) => "(" + a + ", " + b + ")") I get the following error: console:28: error: type mismatch; found : (Int, Int) => String required: ((Int, Int)) => ? x.map((a: Int, b: Int) => "(" + a + ", " + b + ")") Why does it expect the type of the first element to be ((Int, Int)) ? 回答1: (Int, Int) => ... is the type of a function with two arguments, both Int (and that