Tokenize a string: Difference between revisions
Content added Content deleted
(added ocaml) |
|||
Line 215: | Line 215: | ||
splitBy f list = first : splitBy f (dropWhile f rest) where |
splitBy f list = first : splitBy f (dropWhile f rest) where |
||
(first, rest) = break f list |
(first, rest) = break f list |
||
splitRegex :: Regex -> String -> [String] |
|||
joinWith :: [a] -> <nowiki>[[a]]</nowiki> -> [a] |
joinWith :: [a] -> <nowiki>[[a]]</nowiki> -> [a] |
||
joinWith d xs = concat $ List.intersperse d xs |
joinWith d xs = concat $ List.intersperse d xs |
||
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later |
|||
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today" |
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today" |
||
-- using regular expression to split: |
|||
import Text.Regex |
|||
putStrLn $ joinWith "." $ splitRegex (mkRegex ',') $ "Hello,How,Are,You,Today" |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 267: | Line 274: | ||
) |
) |
||
format "%\n" output |
format "%\n" output |
||
=={{header|OCaml}}== |
|||
To split on a single-character separator: |
|||
<ocaml>let rec split_char sep str = |
|||
try |
|||
let i = String.index str sep in |
|||
String.sub str 0 i :: |
|||
split_char sep (String.sub str (i+1) (String.length str - i - 1)) |
|||
with Not_found -> |
|||
[str]</ocaml> |
|||
Splitting on a string separator using the regular expressions library: |
|||
<ocaml>#load "str.cma";; |
|||
let split_str sep str = |
|||
Str.split (Str.regexp_string sep) str</ocaml> |
|||
Joining is a simple folding recursion: |
|||
<ocaml>let rec join_with d = function |
|||
[] -> "" |
|||
| [x] -> x |
|||
| x :: xs -> x ^ d ^ join_with d xs</ocaml> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |