guard-clause

What does left arrow <- mean outside a do block?

*爱你&永不变心* 提交于 2020-11-27 05:02:34
问题 I came across with the following code recently and it bothers me a lot lowerSafeForeignCall dflags block | (entry, middle, CmmForeignCall { .. }) <- blockSplit block = do -- do block stuffs -- Block doesn't end in a safe foreign call: | otherwise = return block This piece of code is from https://phabricator.haskell.org/rGHCb0534f78a73f972e279eed4447a5687bd6a8308e in file compiler/cmm/CmmLayoutStack.hs line 983 I really would like to konw what is this <- in the second line. I believe

What does left arrow <- mean outside a do block?

被刻印的时光 ゝ 提交于 2020-11-27 05:01:45
问题 I came across with the following code recently and it bothers me a lot lowerSafeForeignCall dflags block | (entry, middle, CmmForeignCall { .. }) <- blockSplit block = do -- do block stuffs -- Block doesn't end in a safe foreign call: | otherwise = return block This piece of code is from https://phabricator.haskell.org/rGHCb0534f78a73f972e279eed4447a5687bd6a8308e in file compiler/cmm/CmmLayoutStack.hs line 983 I really would like to konw what is this <- in the second line. I believe

F# pattern match directly against let binding

倾然丶 夕夏残阳落幕 提交于 2020-01-11 10:04:34
问题 Is it possible in F# to pattern match directly against a let binding? For example, this compiles without any warnings: let value = match arg with | 1 -> "value1" | 2 -> "value2" | _ -> failwith "key not found" Whereas the following gives the warning "This rule will never be matched" against the lines matching key2 and _ : let key1 = 1 let key2 = 2 let value = match arg with | key1 -> "value1" | key2 -> "value2" | _ -> failwith "key not found" Is this because although they're immutable, the

Incomplete pattern matching a tuple in F#

孤者浪人 提交于 2019-12-23 20:14:44
问题 I define a point type TimeSeriesPoint<'T> = { Time : DateTimeOffset Value : 'T } and a series type TimeSeries<'T> = TimeSeriesPoint<'T> list where I assume the points in this list are ordered by time. I am trying to zip two time series, where, in general, they will have points with the same time, but there might be some points missing in either of them. Any idea why I get a warning for incomplete pattern matches in the code below? let zip (series1 : TimeSeries<float>) (series2 : TimeSeries

Pattern matching with guards vs if/else construct in F#

对着背影说爱祢 提交于 2019-12-19 06:34:26
问题 In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using if/else (see delete2 ): let rec delete1 (a, xs) = match xs with | [] -> [] | x::xs' -> if x = a then xs' else x::delete1(a, xs') let rec delete2 (a, xs) = match xs with | [] -> [] | x::xs' when x = a -> xs' | x::xs' -> x::delete2(a, xs') Another

Refactoring Guard Clauses

℡╲_俬逩灬. 提交于 2019-12-18 10:33:28
问题 What approaches do people take (if any) in managing guard clause explosion in your classes? For example: public void SomeMethod<T>(string var1, IEnumerable<T> items, int count) { if (string.IsNullOrEmpty(var1)) { throw new ArgumentNullException("var1"); } if (items == null) { throw new ArgumentNullException("items"); } if (count < 1) { throw new ArgumentOutOfRangeException("count"); } ... etc .... } In the project that I am currently working on there are many classes that have a similar set

Refactoring Guard Clauses

感情迁移 提交于 2019-12-18 10:33:07
问题 What approaches do people take (if any) in managing guard clause explosion in your classes? For example: public void SomeMethod<T>(string var1, IEnumerable<T> items, int count) { if (string.IsNullOrEmpty(var1)) { throw new ArgumentNullException("var1"); } if (items == null) { throw new ArgumentNullException("items"); } if (count < 1) { throw new ArgumentOutOfRangeException("count"); } ... etc .... } In the project that I am currently working on there are many classes that have a similar set

Incomplete pattern match when two patterns share a `when` clause

寵の児 提交于 2019-12-13 13:25:49
问题 A common surprise for beginning F# programmers is the fact that the following is an incomplete match: let x, y = 5, 10 match something with | _ when x < y -> "Less than" | _ when x = y -> "Equal" | _ when x > y -> "Greater than" But I just encountered a situation that surprised me. Here's a small bit of sample code to demonstrate it: type Tree = | Leaf of int | Branch of Tree list let sapling = Branch [Leaf 1] // Small tree with one leaf let twoLeafTree = Branch [Leaf 1; Leaf 2] let describe