pattern-matching

How to make fixed-length vectors instance of Applicative?

不想你离开。 提交于 2019-12-08 20:25:13
问题 I recently learned about promotion and decided to try writing vectors. {-# LANGUAGE DataKinds, GADTs, KindSignatures #-} module Vector where data Nat = Next Nat | Zero data Vector :: Nat -> * -> * where Construct :: t -> Vector n t -> Vector ('Next n) t Empty :: Vector 'Zero t instance Functor (Vector n) where fmap f a = case a of Construct x b -> Construct (f x) (fmap f b) Empty -> Empty So far, everything is working. But I ran into a problem when trying to make Vector instance of

How to use regexp on the results of a sub query?

喜夏-厌秋 提交于 2019-12-08 18:29:24
I have two tables. User which has id and phone number id phone_no 1 ---- 9912678 2 ---- 9912323 3 ---- 9912366 Admission Table , which has id phone number id phone_no 6 --- 991267823 7 --- 991236621 8 --- 435443455 9 --- 243344333 I want to find all the phone number of Admission's table which has same pattern as users table and update it in users table. So i am trying this select phone_no from admission where phone_no REGEXP (SELECT phone_no FROM `users` AS user WHERE user.phone_no REGEXP '^(99)+[0-9]{8}') But i am getting this error Subquery returns more than 1 row Looking for help. Try one

Polymorphic pattern matching results in ambiguous type

核能气质少年 提交于 2019-12-08 15:59:12
问题 (Inspired by cannot match on polymorphic tuples with constraints, and based on a subsequent comment on my own answer.) Consider the following minimal example: test :: (Show a, Show b) => (a -> String, b -> String) test = (show,show) (resX, resY) = test This results in the following error: • Ambiguous type variable ‘a0’ arising from a use of ‘test’ prevents the constraint ‘(Show a0)’ from being solved. Relevant bindings include resX :: a0 -> String (bound at so.hs:25:2) Probable fix: use a

F# Power issues which accepts both arguments to be bigints

别来无恙 提交于 2019-12-08 15:29:12
问题 I am currently experimenting with F#. The articles found on the internet are helpful, but as a C# programmer, I sometimes run into situations where I thought my solution would help, but it did not or just partially helped. So my lack of knowledge of F# (and most likely, how the compiler works) is probably the reason why I am totally flabbergasted sometimes. For example, I wrote a C# program to determine perfect numbers. It uses the known form of Euclids proof, that a perfect number can be

In Haskell, what is the scope of a where clause when dealing with guards?

二次信任 提交于 2019-12-08 14:43:23
问题 I know they do not hold across pattern matches (i.e. you need to rewrite the 'where' clause for each pattern), but how does the scoping work for guards? e.g. Does this work? myFunction x1 x2 | x1 > x2 = addOne x1 | x1 < x2 = addOne x2 | otherwise = x1 where addOne = (1+) Or should it be this? myFunction x1 x2 | x1 > x2 = addOne x1 where addOne = (1+) | x1 < x2 = addOne x2 where addOne = (1+) | otherwise = x1 回答1: The first one is the correct one. I would suggest you to have a look at the let

How to pattern match a class with multiple argument lists?

北战南征 提交于 2019-12-08 14:37:59
问题 Consider this class: class DateTime(year: Int, month: Int, day: Int)(hour: Int, minute: Int, second: Int) how would the unapply method look like, if I would like to match against something like: dt match { case DateTime(2012, 12, 12)(12, _, _) => // December 12th 2012, 12 o'clock /* ... */ } I tried this: def unapply(dt: DateTime) = Some((dt.year, dt.month, dt.day),(dt.hour, dt.minute, dt.second)) But that didn't really work. 回答1: Case classes match (and do their other nifty things) only on

Find multiple strings and replace with single string in Postgres

孤街浪徒 提交于 2019-12-08 10:17:26
问题 Is it possible to do a "find and replace" with the following? UPDATE __table__ SET __column__ = replace(__column__, ' ', '_'); How do I define an array of strings to be found ( ',' , ';' , ':' , ' ' ) to replace with '_' ? 回答1: Read the section about Bracket Expressions which explains how to search for characters within a string to replace but this should work for you UPDATE __table__ SET __column__ = regexp_replace( __column__, E'[\\s,;:]','_','g') 回答2: regexp_replace() is powerful,

Phrase matching using regex and Python

蹲街弑〆低调 提交于 2019-12-08 10:15:37
问题 I have some short phrases that I want to match on. I used a regex as follows: (^|)(piston|piston ring)( |$) Using the above, regex.match("piston ring") matches on "piston". If I change the regex such that the longer phrase "piston ring" comes first then it work as expected. I was surprised by this behavior as I was assuming that the greedy nature of regex would try to match the longest string "for free." What am I missing? Can somebody explain this? Thanks! 回答1: When using alternation ( | )

Loop to perform calculations across rows on specific columns matching a pattern (in data frame)?

血红的双手。 提交于 2019-12-08 09:43:58
问题 I have a dataframe with some boolean values (1/0) as follows (sorry I couldn't work out how to make this into a smart table) Flag1.Sam Flag2.Sam Flag3.Sam Flag1.Ted Flag2.Ted Flag3.Ted probe1 0 1 0 1 0 0 probe2 0 0 0 0 0 0 probe3 1 0 0 0 0 0 probe4 0 0 0 0 0 0 probe5 1 1 0 1 0 0 I have 64 samples (Sam/Ted....etc) which are in a list called files i.e; files <- c("Sam", "Ted", "Ann", ....) And I would like to create a a column summing the flag values for each sample to create the following: Sam

obtaining 2d-3d point correspondences for pnp or posit

試著忘記壹切 提交于 2019-12-08 09:37:17
问题 I am trying to estimate the pose and position of a satellite given an image of it. I have a 3D model of the satellite. Using either PnP solvers or POSIT works great when I pick out the point correspondences myself, however I need to to find a method to match the points up automatically. Using a corner detector (best one I found so far is based on the contour) I can find all the relevant points in the image in addition a few spurious points. However I need to match a given point in the image