How to remove currency amounts from a list? ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"] -> ["Hello World"] - currency

["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"] -> ["Hello World"]
I was thinking of using regex, but is there a more easy way?

Assuming this is Python, a simple List Comprehension should suffice:
lst = ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"]
[el for el in lst if "€" not in el]
>>> ["Hello World"]
Another option is using the filter() function:
def contains_currency(s):
return "€" not in s
list(filter(contains_currency, lst))
more compactly using an anonymous function:
list(filter(lambda s: "€" not in s, lst))

Assuming this is Javascript, a simple filter() should suffice:
let array = ["9.358,26 €", "Hello World", "3.562,77 €", "3,77 €"];
filteredArray = array.filter((item) => !item.includes('€'));
console.log(filteredArray);
// ["Hello World"]
Read more about the includes() method here.

Related

How do i apply both hard-coded and method classes in Elmish?

I am formatting a web application made using F# and the SAFE stack. I am using a variable to determine a CSS class to change the formatting of a tag, but I also need two hard-coded CSS classes, and I am unsure how to have both.
I have this:
let statusTag (state:Appointment.State) =
span [ Class (state.ToString()) ] [ str (sprintf "%A" state) ]
And i need it to work more like this:
let statusTag (state:Appointment.State) =
span [ Class "status text" + (state.ToString()) ] [ str (sprintf "%A" state) ]
But i dont know how to do this in F#
Any help would be appreciated
The only thing that seems wrong with your attempt is that you need extra parentheses around the expression that constructs the string with the names of the classes (on the other hand, you do not need it around the state.ToString() call). The following should do the trick:
let statusTag (state:Appointment.State) =
span [ Class("status text" + state.ToString()) ] [ str (sprintf "%A" state) ]

How can I use a parser in Haskell to find the locations of some substrings in a string?

I'm very new to Haskell. I'd like to be able to find some color expressions in a string. So let's say I have this list of expressions:
colorWords = ["blue", "green", "blue green"]
And I want to be able to get the locations of all of those, anywhere in a string, even if it's broken up by a linebreak, or if a hyphen separates it instead. So given a string like:
First there was blue
and then there was Green,
and then blue
green all of a sudden, and not to mention blue-green
It should give the character offsets for "blue" (line one), "green" (line two), and "blue green" (lines 3-4) and "blue-green" (line 4), something like:
[("blue", [20]), ("green", [40]), ("blue green", [50, 65])]
I can do this with regexes, but I've been trying to do it with a parser just as an exercise. I'm guessing it's something like:
import Text.ParserCombinators.Parsec
separator = spaces <|> "-" <|> "\n"
colorExp colorString = if (length (words colorString))>1 then
multiWordColorExp colorString
else colorString
multiWordColorExp :: Parser -> String
multiWordColorExp colorString = do
intercalate separator (words colorString)
But I have no idea what I'm doing, and I'm not really getting anywhere with this.
We can find substring locations with a parser by using the sepCap combinator from replace-megaparsec.
Here's a solution to your example problem. Requires packages megaparsec, replace-megaparsec, containers.
References:
string'
choice
getOffset
try
from Megaparsec.
import Replace.Megaparsec
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Maybe
import Data.Either
import Data.Map.Strict as Map
let colorWords :: Parsec Void String (String, [Int])
colorWords = do
i <- getOffset
c <- choice
[ try $ string' "blue" >>
anySingle >>
string' "green" >>
pure "blue green"
, try $ string' "blue" >> pure "blue"
, try $ string' "green" >> pure "green"
]
return (c,[i])
input = "First there was blue\nand then there was Green,\nand then blue\ngreen all of a sudden, and not to mention blue-green"
Map.toList $ Map.fromListWith mappend $ rights $ fromJust
$ parseMaybe (sepCap colorWords) input
[("blue",[16]),("blue green",[103,56]),("green",[40])]

Add two variables such that they become a String with a space between in Ruby

So i am trying to output in the same line two string variables with a space but I am getting the following error:
in `+': no implicit conversion of Fixnum into String (TypeError)
for the following code
puts s1 + " " + s2
How can I fix this code to print the value of string variable s1 with a space and the value of string variable s2 on the same line? for example
s1 = "hello" s2="world" output "Hello world"
You can do it with the following:
puts "#{s1} #{s2}"
and:
puts [s1, s2].join(" ")
Here are the following examples implemented code snippets above:
s1, s2 = "hello", "world"
puts "#{s1} #{s2}"
=> "hello world"
puts [s1, s2].join(" ")
=> "hello world"
Try:
puts s1.to_s + " " + s2.to_s
It looks like at least one of your variables contains a numerical value (int)

How to parse a sequence of " " separated values in Haskell

I am a beginner in Haskell and i need to parse a sequence of valuses spearate by something.
The following parser is generated with tokenparser:
m_semiSep1 p parses and returns a semicolon-separated sequence of one or more p's.
But i dont quite understand how it is created. I need one that returns a comma separated sequence of p`s. Can you give me a hint how can i do that. I also need to parse a sequence of "=|" separated values but i suppose that it will be the same as with the comma. This is the code i am working on:
def = emptyDef{ commentStart = "{-"
, commentEnd = "-}"
, identStart = letter
, identLetter = alphaNum
, opStart = oneOf "^~&=:-|,"
, opLetter = oneOf "^~&=:-|,"
, reservedOpNames = ["~", "&", "^", ":=", "|-", ","]
, reservedNames = ["true", "false", "nop",
"if", "then", "else", "fi",
"while", "do", "od"]
}
TokenParser{ parens = m_parens
, identifier = m_identifier
, reservedOp = m_reservedOp
, reserved = m_reserved
, semiSep1 = m_semiSep1
, whiteSpace = m_whiteSpace } = makeTokenParser def
You can use sepBy in parsec. sepBy cell deli parses something like cell deli cell deli...
For example:
Prelude> :m Text.ParserCombinators.Parsec
Prelude Text.ParserCombinators.Parsec> let csv = (many letter) `sepBy` (char ',') :: Parser [String]
Prelude Text.ParserCombinators.Parsec> parse csv "" "xx,yy,zz"
Right ["xx","yy","zz"]
https://hackage.haskell.org/package/parsec-3.1.9/docs/Text-Parsec-Combinator.html#v:sepBy

Concisely creating an IDictionary<_,obj>

Is there a shorter way of creating an IDictionary<_,obj>, possibly without boxing every value? This is what I have.
let values =
[ "a", box 1
"b", box "foo"
"c", box true ]
|> dict
Dictionary<_,obj>.Add can be called without boxing, but I couldn't figure out a way to use it that's shorter than what I have.
I'm hoping for something other than defining a boxing operator.
EDIT
Based on Brian's suggestion, here's one way to do it, but it has its own problems.
let values =
Seq.zip ["a"; "b"; "c"] ([1; "foo"; true] : obj list) |> dict
Here's a solution, following kvb's suggestion (probably the most concise, and clearest, so far):
let inline (=>) a b = a, box b
let values =
[ "a" => 1
"b" => "foo"
"c" => true ]
|> dict
Here's the slickest thing I was able to whip up. It has more characters than your boxing version, but possibly feels a little less dirty. Note that the ^ is right-associative (it's the string concat operator inherited from ocaml), which lets it work like ::, and it has stronger precedence than ,, which is why the parenthesis are needed around the tuples.
let inline (^+) (x1:'a,x2:'b) (xl:('a*obj) list) =
(x1,box x2)::xl
let values =
("a", 1) ^+ ("b", "foo") ^+ ("c", true) ^+ []
|> dict
I had a similar problem in FsSql and I just tucked away boxing in a function:
let inline T (a,b) = a, box b
let values = dict [T("a",1); T("b","foo"); T("c",true)]
Here's another "solution" which is inspired from Brian's suggestion but it uses reflection so there is a time and safety cost.
let unboxPair (pair:obj) =
let ty = pair.GetType()
let x = ty.GetProperty("Item1").GetValue(pair,null) :?> string
let y = ty.GetProperty("Item2").GetValue(pair,null)
x,y
let unboxPairs (pairs:obj list) =
pairs |> List.map unboxPair
let values =
unboxPairs
["a", 1
"b", "foo"
"c", true]
|> dict
A variation of Stephen's idea:
open System
open System.Collections.Generic
type Dictionary<'a,'b> with
member this.Add([<ParamArray>] args:obj[]) =
match args.Length with
| n when n % 2 = 0 ->
for i in 1..2..(n-1) do
this.Add(unbox args.[i-1], unbox args.[i])
| _ -> invalidArg "args" "even number of elements required"
let d = Dictionary<string,obj>()
d.Add(
"a", 1,
"b", "foo",
"c", true
)
Yet another solution, simply define a bunch of overloaded extension members on Dictionary<'a,'b>:
open System.Collections.Generic
type Dictionary<'a,'b> with
member this.Add(x1,y1,x2,y2) =
this.Add(x1,y1)
this.Add(x2,y2)
member this.Add(x1,y1,x2,y2,x3,y3) =
this.Add(x1,y1,x2,y2)
this.Add(x3,y3)
member this.Add(x1,y1,x2,y2,x3,y3,x4,y4) =
this.Add(x1,y1,x2,y2,x3,y3)
this.Add(x4,y4)
member this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5) =
this.Add(x1,y1,x2,y2,x3,y3,x4,y4)
this.Add(x5,y5)
member this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) =
this.Add(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)
this.Add(x6,y6)
//etc.
let values =
let d = Dictionary<_,obj>()
d.Add("a", 1,
"b", "foo",
"c", true)
d
Of course values here is not immutable like in your question, but I'm sure you could employ the same strategy in that goal.
let v : (string*obj) list = [...]
let values = dict v
Is one way, the type signature on the left of the list literal will auto-upcast each element.

Resources