combinators

How to encode a constraint on the format of String values

笑着哭i 提交于 2020-01-17 02:53:16
问题 As I frequently observe and how I often implement a name attribute, is to simply model it as String . What now, if the name has to follow a certain syntax, i.e. format? In Java I probably would define a constructor with a check on its arguments, something like: public Name(str: String) { if (str == null) throw new IllegalArgumentException("Str must not be null."); if (!str.matches("name format expressed as regex")) throw new IllegalArgumentException("Str must match 'regex' but was " + str);

Can XOR be expressed using SKI combinators?

那年仲夏 提交于 2020-01-15 03:39:24
问题 I have question about SKI-Combinators. Can XOR (exclusive or) be expressed using S and K combinators only? I have True = Cancel False = (Swap Cancel) where Cancel x y = K x y = x Swap: ff x y = S ff x y = ff y x 回答1: Booleans Your question is a bit unclear on the details, but it seems that what you mean is that you have the following representation of booleans: T := K F := S K This works because it means the following reductions hold: T t e => t F t e => e in other words, b t e can be

Combine a PartialFunction with a regular function

孤街醉人 提交于 2020-01-14 13:56:09
问题 So, suppose, I want to provide a "catch all" fall back for a PartialFunction : val foo: PartialFunction[Int, String] = { case 1 => "foo" } val withDefault = foo orElse { _.toString } This does not compile: missing parameter type for expanded function ((x$1) => x$1.toString) . This: val withDefault = foo orElse { case x: Int => x.toString } Does not compile either (same error). This: val withDefault = foo orElse { (x: Int) => x.toString } fails with type mismatch; found : Int => String;

Explain this implementation of the Y combinator in Scala?

最后都变了- 提交于 2020-01-11 19:59:26
问题 This is a implementation of the Y-combinator in Scala: scala> def Y[T](func: (T => T) => (T => T)): (T => T) = func(Y(func))(_:T) Y: [T](func: (T => T) => (T => T))T => T scala> def fact = Y { | f: (Int => Int) => | n: Int => | if(n <= 0) 1 | else n * f(n - 1)} fact: Int => Int scala> println(fact(5)) 120 Q1: How does the result 120 come out, step by step? Because the Y(func) is defined as func(Y(func)) , the Y should become more and more,Where is the Y gone lost and how is the 120 come out

convert flip lambda into SKI terms

百般思念 提交于 2020-01-02 06:58:24
问题 I'm having trouble converting the lambda for flip into the SKI combinators (I hope that makes sense). Here is my conversion: /fxy.fyx /f./x./y.fyx /f./x.S (/y.fy) (/y.x) /f./x.S f (/y.x) /f./x.S f (K x) /f.S (/x.S f) (/x.K x) /f.S (/x.S f) K /f.S (S (/x.S) (/x.f)) K /f.S (S (K S) (K f)) K S (/f.S (S (K S) (K f))) (/f.K) S (/f.S (S (K S) (K f))) (K K) S (S (/f.S) (/f.S (K S) (K f))) (K K) S (S (K S) (/f.S (K S) (K f))) (K K) S (S (K S) (S (/f.S (K S)) (/f.K f))) (K K) S (S (K S) (S (/f.S (K S)

Self-error-dependent self-disposal of IObservable subscriptions

霸气de小男生 提交于 2019-12-25 05:04:26
问题 I have F# code that looks like this: module O = Control.Observable //... use ss = serve' 4000 |> O.subscribe (fun c -> use cs = RSS.items |> O.subscribe (bytes >> c.SendAll) |> ignore) where serve' : int -> IObservable<Socket> c : Socket RSS.items : IObservable<XElement> bytes : XElement -> byte [] c.SendAll : byte [] -> unit What is the most idiomatic way to retain cs until c.SendAll fails? Is there or is it possible to define Observable.subscribeUntilError(action) where if action fails,

What are some interesting uses of higher-order functions?

≡放荡痞女 提交于 2019-12-20 08:01:18
问题 I'm currently doing a Functional Programming course and I'm quite amused by the concept of higher-order functions and functions as first class citizens. However, I can't yet think of many practically useful, conceptually amazing, or just plain interesting higher-order functions. (Besides the typical and rather dull map , filter , etc functions). Do you know examples of such interesting functions? Maybe functions that return functions, functions that return lists of functions (?), etc. I'd