functional-programming

The Seasoned Schemer, letcc and guile

强颜欢笑 提交于 2020-01-10 19:16:04
问题 A few questions here, regarding letcc that is used in The Seasoned Schemer. (define (intersect-all sets) (letcc hop (letrec ((A (lambda (sets) (cond ((null? (car sets)) (hop '()) ((null? (cdr sets)) (car sets)) (else (intersect (car sets) (A (cdr sets))))))) ; definition of intersect removed for brevity (cond ((null? sets) '()) (else (A sets)))))) I think I understand what letcc achieves, and that is basically something like catch and throw in ruby (and seemingly CL), which basically means a

Apply a list of functions to a value in Ramda

不想你离开。 提交于 2020-01-10 18:42:06
问题 How would I best create this function in Ramda? function get_list (value) { return [ first_transform(value), second_transform(value) ] } get_list(12) I guess this is the inverse of the map function. 回答1: You've got a few options for this. Assuming your functions are already in a list: transforms = [first_transform, second_transform]; The first option is to use R.juxt, which does pretty much exactly what you're after by creating a new function that applies the list of given functions to the

Why can you only prepend to lists in functional languages?

北慕城南 提交于 2020-01-10 17:30:27
问题 I have only used 3 functional languages -- scala, erlang, and haskell, but in all 3 of these, the correct way to build lists is to prepend the new data to the front and then reversing it instead of just appending to the end. Of course, you could append to the lists, but that results in an entirely new list being constructed. Why is this? I could imagine it's because lists are implemented internally as linked lists, but why couldn't they just be implemented as doubly linked lists so you could

Query on Booleans in Lambda Calculus

丶灬走出姿态 提交于 2020-01-10 14:14:29
问题 This is the lambda calculus representation for the AND operator: lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b Can anyone help me in understanding this representation? 回答1: To understand how to represent Booleans in lambda calculus, it helps to think about an IF expression, "if a then b else c". This is an expression which chooses the first branch, b, if it is true, and the second, c, if it is false. Lambda expressions can do that very easily: lambda(x).lambda(y).x will give you the

Scala: How to create a Map[K,V] from a Set[K] and a function from K to V?

▼魔方 西西 提交于 2020-01-10 10:14:32
问题 What is the best way to create a Map[K,V] from a Set[K] and function from K to V ? For example, suppose I have scala> val s = Set(2, 3, 5) s: scala.collection.immutable.Set[Int] = Set(2, 3, 5) and scala> def func(i: Int) = "" + i + i func: (i: Int)java.lang.String What is the easiest way of creating a Map[Int, String](2 -> "22", 3 -> "33", 5 -> "55") 回答1: You can use foldLeft : val func2 = (r: Map[Int,String], i: Int) => r + (i -> func(i)) s.foldLeft(Map.empty[Int,String])(func2) This will

What is the advantage of using scala pattern matching instead of java switch case?

喜你入骨 提交于 2020-01-09 19:32:35
问题 Everybody says that pattern matching is a great feature in functional languages. Why? Can't I simple use ifs and switch cases for everything? I'd like to understand the advantages of using pattern matching instead of regular procedural programming ifs and switch cases 回答1: I'd first like to note that you don't use pattern matching "instead" of switch statements. Scala doesn't have switch statements, what it does have is match blocks, with cases inside that superficially look very similar to a

What is the advantage of using scala pattern matching instead of java switch case?

落爺英雄遲暮 提交于 2020-01-09 19:30:33
问题 Everybody says that pattern matching is a great feature in functional languages. Why? Can't I simple use ifs and switch cases for everything? I'd like to understand the advantages of using pattern matching instead of regular procedural programming ifs and switch cases 回答1: I'd first like to note that you don't use pattern matching "instead" of switch statements. Scala doesn't have switch statements, what it does have is match blocks, with cases inside that superficially look very similar to a

Validation versus disjunction

亡梦爱人 提交于 2020-01-09 04:22:10
问题 Suppose I want to write a method with the following signature: def parse(input: List[(String, String)]): ValidationNel[Throwable, List[(Int, Int)]] For each pair of strings in the input, it needs to verify that both members can be parsed as integers and that the first is smaller than the second. It then needs to return the integers, accumulating any errors that turn up. First I'll define an error type: import scalaz._, Scalaz._ case class InvalidSizes(x: Int, y: Int) extends Exception( s

languageext Either.Map/Bind with a Task in the Right position

冷暖自知 提交于 2020-01-07 06:18:47
问题 I am using the toolkit languageext package for C# and am running into a problem with the Either class when the Right value is some kind of Task. For some reason this is causing a hang: var res = repo.GetAccountWithID(accountID) .Map(c => filesServiceCustomer.Initialize(c)) .Bind(t => t.Result); Here, GetAccountWithID returns an Either<Exception, Account> and the Initialize method take an Account and returns a Task<Either<Exception, bool>> . However, it would appear that either the Map or Bind

Scala Future not entering onComplete()

五迷三道 提交于 2020-01-07 04:20:36
问题 I am trying to grab the value from a snippet of code that results in a Future , but when I run through the code, it turns out that it never enters the onComplete function. Is there anything I'm doing wrong? I've tried using a map as well since it was suggested in other posts, but haven't had success override def findOrCreate(phoneNumber: String, creationReason: String): Future[AvroCustomer] = { //query for customer in db val avroCustomer: Future[AvroCustomer] = customerByPhone(phoneNumber)