pattern-matching

Is it a rule that unapply will always return an Option?

末鹿安然 提交于 2020-01-14 08:40:28
问题 I tried to create an unapply method to use in pattern matching, and I tried to make it return something different than Option , however, Eclipse shows that as an error. Is it a rule that unapply must return an Option[T] ? EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply returns a Boolean import java.util.regex._ object NumberMatcher { def apply(x:String):Boolean = { val pat = Pattern.compile("\\d+") val matcher = pat.matcher(x) return

Simplifying nested Maybe pattern matching

余生长醉 提交于 2020-01-13 05:21:08
问题 I have the following construct in my code: f :: Maybe A -> X f a = case a of Nothing -> x (Just b) -> case b of Nothing -> y (Just c) -> case c of Nothing -> z (Just d) -> d I'm not seeing an obvious way to simplify this instead of using nested maybe functions, which wouldn't make the whole thing look much better. Are there any clever - but still understandable - tricks that would help make this construct more "elegant"? 回答1: UPDATED 2 Monad Either is for you import Data.Maybe (maybe) maybeE

Counting object on image algorithm

一个人想着一个人 提交于 2020-01-13 01:55:01
问题 I got school task again. This time, my teacher gave me task to create algorithm to count how many ducks on picture. The picture is similar to this one: I think I should use pattern recognition for searching how many ducks on it. But I don't know which pattern match for each duck. 回答1: Here's one way: Hough transform for circles: Initialize an accumulator array indexed by (x,y,radius) For each pixel: calculate an edge (e.g. Sobel operator will provide both magnitude and direction), if

How does pattern matching work behind the scenes in F#?

∥☆過路亽.° 提交于 2020-01-12 06:41:22
问题 I am completely new to F# (and functional programming in general) but I see pattern matching used everywhere in sample code. I am wondering for example how pattern matching actually works? For example, I imagine it working the same as a for loop in other languages and checking for matches on each item in a collection. This is probably far from correct, how does it actually work behind the scenes? 回答1: It depends on what kind of pattern matching do you mean - it is quite powerful construct and

About the pattern matching algorithm in OCaml

点点圈 提交于 2020-01-12 05:42:10
问题 I am writing a compiler for a functional language I designed with OCaml. I want my little language to have the feature of pattern matching, however, I got stuck in coming up with an algorithm to implement it. It seems really complicated as I dig into the problem. I can't find much useful information about the corresponding algorithm with google. I will be appreciated if someone can give me some hint or point me to the resources. Or are there any tricks to take advantage of OCaml's power in

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

Python read through file until match, read until next pattern

女生的网名这么多〃 提交于 2020-01-11 09:44:19
问题 Python 2.4.3 I need to read through some files (can be as large as 10GB). What I need it to do is go through the file until it matches a pattern. Then print that line and every line after it until it matches another pattern. At that point, resume reading through the file until the next pattern match. For example. file contains. ---- Alpha ---- Zeta ...(text lines) ---- Bravo ---- Delta ...(text lines) etc. If matching on ---- Alpha ---- Zeta, it should print ---- Alpha ---- Zeta and every

Replace value in column with corresponding value from another column in same dataframe

眉间皱痕 提交于 2020-01-11 06:47:51
问题 Im trying to match a specific value in one column and replace it with the corresponding value from another column (same row). This is probably very easy... I have been trying to find a solution with for loop , sub , subset , data.table but I have not succeeded. There must be a neat way of doing this. Example data, where we aim at swapping a in the first column with the corresponding value in the second column and outputting the column again. df <- data.frame(rbind(c('a','D'),c('H','W'),c('Q',

Pattern match for variable in scope (Scala)

放肆的年华 提交于 2020-01-11 02:50:26
问题 In the following code val x = 5 val y = 4 match { case x => true case _ => false } the value y is true. Scala interprets x to be a free variable in the pattern match instead of binding it to the variable with the same name in the scope. How to solve this problem? 回答1: Invoking the least astonishment principle , I will simply do: val x = 5 val y = 4 match { case z if z == x => true case _ => false } 回答2: Backticking the variable indicates to bind a scoped variable: val x = 5 val y = 4 match {

Combining two lists in Scala

孤街浪徒 提交于 2020-01-10 20:52:07
问题 From 2 lists of the form List[(Int, String) : l1 = List((1,"a"),(3,"b")) l2 = List((3,"a"),(4,"c")) how can I combine the Integer s where the String s are the same to get this third list: l3 = List((4,"a"),(3,"b"),(4,"c")) Right now I'm traversing both of the lists and adding if the strings are the same, but I think there should be a simple solution with pattern matching. 回答1: val l = l1 ::: l2 val m = Map[String, Int]() (m /: l) { case (map, (i, s)) => { map.updated(s, i + (map.get(s)