hey i was wandering if it was possible to show a list:
["one", "two", "three"]
to be shown as
"one", "two", "three"
need it done for a file
thanks
You can do this with intercalate from Data.List
showList :: Show a => [a] -> String
showList = intercalate ", " . map show
The map show converts each element to it's string representation with quotes (and any internal quotes properly escaped), while intercalate ", " inserts commas and spaces between the pieces and glues them together.
Related
I would love to take an array of strings
let array = ["one", "two", "three", "four"]
and convert it to the regex builder equivalent of:
Regex {
ChoiceOf{
"one"
"two"
"three"
"four"
}
}
or basically the equivelant of:
/one|two|three|four/
so far I have tried:
let joinedArray = array.joined(separator: "|")
let choicePattern = Regex(joinedArray)
I know that using Regex() throws and I need to handle that somehow but even when I do, I don't seem to get it to work.
Does anyone know how to do this?
I am looking for efficient solution to remove empty string in a list in Julia.
Here is my list :
li = ["one", "two", "three", " ", "four", "five"]
I can remove empty string by using for loop, as following :
new_li = []
for i in li
if i == " "
else
push!(new_li, i)
end
end
But I believe there is more efficient way to remove the empty string.
new_li = filter((i) -> i != " ", li)
or
new_li = [i for i in li if i != " "]
I have a couple of comments a bit too long for the comments field:
Firstly, when building an array, never start it like this:
new_li = []
It creates a Vector{Any}, which can harm performance. If you want to initialize a vector of strings, it is better to write
new_li = String[]
Secondly, " " is not an empty string! Look here:
jl> isempty(" ")
false
It is a non-empty string that contains a space. An empty string would be "", no space. If you're actually trying to remove empty strings you could do
filter(!isempty, li)
or, for in-place operation, you can use filter!:
filter!(!isempty, li)
But you're not actually removing empty strings, but strings consisting of one (or more?) spaces, and maybe also actually empty strings? In that case you could use isspace along with all. This will remove all strings that are only spaces, including empty strings:
jl> li = ["one", "", "two", "three", " ", "four", " ", "five"];
jl> filter(s->!all(isspace, s), li)
5-element Vector{String}:
"one"
"two"
"three"
"four"
"five"
input file is txt :
000011S\n
0001110\n
001G111\n
0001000\n
Result is:
[["0","0","0","0","1","1","S"], ["0","0","0","1","1","1","0"] [...]]
Read a text file with
file <- openFile nameFile ReadMode
and the final output
[["a","1","0","b"],["d","o","t","2"]]
is a map with list of char
try to:
convert x = map (map read . words) $ lines x
but return [[string ]]
As it could do to return the output I want? [[Char]],
is there any equivalent for word but for char?
one solution
convert :: String -> [[String]]
convert = map (map return) . lines
should do the trick
remark
the return here is a neat trick to write \c -> [c] - wrapping a Char into a singleton list as lists are a monad
how it works
Let me try to explain this:
lines will split the input into lines: [String] which each element in this list being one line
the outer map (...) . lines will then apply the function in (...) to each of this lines
the function inside: map return will again map each character of a line (remember: a String is just a list of Char) and will so apply return to each of this characters
now return here will just take a character and put it into a singleton list: 'a' -> [a] = "a" which is exactly what you wanted
your example
Prelude> convert "000011S\n0001110\n001G111\n0001000\n"
[["0","0","0","0","1","1","S"]
,["0","0","0","1","1","1","0"]
,["0","0","1","G","1","1","1"]
,["0","0","0","1","0","0","0"]]
concerning your comment
if you expect convert :: String -> [[Char]] (which is just String -> [String] then all you need is convert = lines!
[[Char]] == [String]
Prelude> map (map head) [["a","1","0","b"],["d","o","t","2"]]
["a10b","dot2"]
will fail for empty Strings though.
or map concat [[...]]
How do I convert a list of strings into a string in DrRacket? For example, if I have
'("44" "444") convert it into "44 444"?
I tried string-join, but it takes a delimiter and if I put one it replaces the space with the delimiter and if I use "" for the delimiter it simply gets rid of it.
In fact string-join is the right procedure for using in this case, simply use " " (a single space) as delimiter:
(string-join '("44" "444") " ")
=> "44 444"
Just to clarify: in a list the spaces between elements are not considered part of the list, they're there to separate the elements. For example, all these lists are equal and evaluate to the same value:
'("44""444")
'("44" "444")
'("44" "444")
If for some reason you want to consider the spaces as part of the list then you have to explicitly add them as elements in the list:
(define lst '("a" " " "b" " " "c" " " "d"))
(string-join lst "")
=> "a b c d"
VIM: Does anyone know how to put a string from an input dialog in a list?
p.e.:
the string "3,5,12,15"
to:
list item[1] = 3
list item[2] = 5
list item[3] = 12
etc.
and how can I know how many list items there are?
From :h E714
:let l = len(list) " number of items in list
:let list = split("a b c") " create list from items in a string
In your case,
let string = "3,5,7,19"
let list = split(string, ",")
echo len(list)
Use split, len and empty functions:
let list=split(string, ',')
let list_length=len(list)
" If all you want is to check whether list is empty:
if empty(list)
throw "You must provide at least one value"
endif
Note that if you want to get a list of numbers out of the string, you will have to use map to transform list elements into numbers:
let list=map(split(string, ','), '+v:val')
Most of time you can expect strings be transformed into numbers, but sometimes such transformation is not done.