pattern-matching

Variable is unbound when used to pattern match as a key in a map

試著忘記壹切 提交于 2019-12-11 06:03:22
问题 I want to pattern match a map on a key when the second argument is a map with name 's value being the same as the key in the first argument's map. Instead I get this: 8> c(room). room.erl:23: variable 'PlayerName' is unbound error status({_Players, _Tables, ChallengerName}, #{name := ChallengerName}) -> #{status => challenging}; status({#{PlayerName := PlayerStatus}, _, _}, #{name := PlayerName}) -> PlayerStatus; status(_, _) -> #{status => null}. Oddly, the first function declaration works

Searching for a word in a grid

浪子不回头ぞ 提交于 2019-12-11 05:48:21
问题 I'm trying to write a function that takes a square grid of letters and given a word to find from a list of words, it searches for it horizontally, vertically or diagonally (also looking backwards in each case). I've tried writing this function in various ways with no success so was wondering if my general algorithm sounded ok and implementable. Return co-ordinates for everywhere the first letter of the word occurs so something like [row,col] = find(grid==words(2)) with words being the list of

Redis OR check in MATCH

不羁的心 提交于 2019-12-11 05:42:21
问题 As a beginner, I would like to know if there is a way to write below query in redis Scan 0 MATCH Test:[keyword*:* | *:keyword] Right now I have to do two separate scans for this query as below Scan 0 MATCH Test:*:keyword* Scan 0 MATCH Test:keyword*:* 回答1: I see that you've 3 choices here: Wrap both commands into a Lua script and call it as it would be a single command. Instead of scanning do indexing . Create a set or sorted set where you'll store all elements that matches these patterns, and

Functor fmap, pattern match function values, haskell

社会主义新天地 提交于 2019-12-11 05:29:14
问题 I have the following type, and would like to make it a Functor: newtype SubsM a = SubsM {runSubsM :: Context -> Either Error (a, Env)} So far i got this instance Functor SubsM where fmap f (SubsM a) = SubsM (\s->(Right((f a),(fst s)))) I get an error because a is not the expected type, my question is how do i pattern match a on the left-hand side? 回答1: You can pattern match on the Either Error (a, Env) with case : instance Functor SubsM where fmap f (SubsM cf) = SubsM $ \c -> case (cf c) of

AI/Deep Learning approach to judge reference in paper?

删除回忆录丶 提交于 2019-12-11 05:08:08
问题 This is a real part of my work. In order to keep students' paper follow given format standard, I have to judge the reference type in students' papers, separate its items(author, title, magazine name, year, etc), then give modification advice if some item is missing. It is tedious, so after some years' of work-by-people, I get too tired. I think to do it by programming. In a paper, many kinds of references will be citation, for example, Journal paper, Dissertation paper, book and so on. They

How to represent algebraic data types and pattern matching in JavaScript

倾然丶 夕夏残阳落幕 提交于 2019-12-11 04:47:54
问题 In functional language like OCaml, we have pattern matching. For example, I want to log users' actions on my website. An action could be 1) visiting a web page, 2) deleting an item, 3) checking the profile of another user, etc. In OCaml, we can write something as follows: type Action = | VisitPage of string (* www.myweb.com/help *) | DeletePost of int (* an integer post id *) | ViewUser of string (* a username *) However, I am not sure how to define this Action in JavaScript. One way I could

CLOSED!! How i can detect the type from a string in Scala?

空扰寡人 提交于 2019-12-11 04:04:21
问题 I'm trying to parse the csv files and I need to determine the type of each field starting from its string value. for examples: val row: Array[String] = Array("1/1/06 0:00","3108 OCCIDENTAL DR","3","3C","1115") this is what I would get: row(0) --> Date row(1) --> String row(2) --> Int Ecc.... how can I do? ------------------------------------ SOLUTION ------------------------------------ This is the solution I've found to recognize the fields String, Date, Int, Double and Boolean. I hope that

regular expression for content within braces

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:48:32
问题 is there a regular expression to match the content within braces. For example with the following: d = {'key': {'a': [1,2,3]}} I would want to match {'key': {'a': [1,2,3]}} and {'a': [1,2,3]} , but not {'key': {'a': [1,2,3]} 回答1: In classical regular expressions, this is impossible - DFAs can't parse nested pairs. There are ways to do it with extended regular expressions, such as recursive expressions that are allowed in some regex engines (such as Perl regex), but they're not always pretty. (

scala : Match type argument for an object

怎甘沉沦 提交于 2019-12-11 03:46:59
问题 if i have a class that accepts a Type argument for example Seq[T] , and i've many objects of this class. and i want to split them depending on type Argument T for example : val x = List(Seq[Int](1,2,3,4,5,6,7,8,9,0),Seq[String]("a","b","c")) x.foreach { a => a match{ case _ : Seq[String] => print("String") case _ : Seq[Int] => print("Int") } } the result of this code is StringString . it only matches the class Seq not the Type also , what should i do to force it to match the Type ? 回答1: What

Pattern matching by function call

邮差的信 提交于 2019-12-11 03:43:41
问题 F# assigns function arguments via pattern matching. This is why // ok: pattern matching of tuples upon function call let g (a,b) = a + b g (7,4) works: The tuple is matched with (a,b) and a and b are available directly inside f. Doing the same with discriminated unions would be equally beneficial, but I cannot get it to done: // error: same with discriminated unions type A = | X of int * int | Y of string let f A.X(a, b) = a + b // Error: Successive patterns // should be separated by spaces