Skip to content

Commit

Permalink
update splitting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacvando committed Nov 29, 2024
1 parent 2fc0ac5 commit 10300ce
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/alphametics/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ parse : Str -> Result { addends : List (List U8), sum : List U8 } _
parse = \problem ->
{ before, after } = Str.splitFirst? problem " == "
addends =
Str.split before " + "
Str.splitOn before " + "
|> List.map Str.toUtf8
Ok { addends, sum: Str.toUtf8 after }
5 changes: 2 additions & 3 deletions exercises/practice/connect/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ parse : Str -> Result Board [InvalidCharacter U8]
parse = \boardStr ->
boardStr
|> Str.trim
|> Str.split "\n"
|> Str.toUtf8
|> List.splitOn '\n'
|> List.mapTry \row ->
row
|> Str.toUtf8
|> List.dropIf \char -> char == ' '
|> List.mapTry \char ->
when char is
Expand Down Expand Up @@ -99,4 +99,3 @@ hasNorthSouthPath = \board, stone ->
Empty -> Err NotPlayerStone
|> List.keepOks \id -> id
hasPathToSouth northStones (Set.empty {})

6 changes: 3 additions & 3 deletions exercises/practice/forth/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ step = \stack, op ->
# Parsing
parse : Str -> Result (List Op) _
parse = \str ->
when Str.split (Str.trim str) "\n" is
when Str.splitOn (Str.trim str) "\n" is
[.. as defLines, program] ->
defs = parseDefs? defLines

Str.split program " "
Str.splitOn program " "
|> flattenDefs defs
|> List.mapTry toOp

Expand All @@ -115,7 +115,7 @@ parse = \str ->
parseDefs : List Str -> Result Defs _
parseDefs = \lines ->
List.walkTry lines (Dict.empty {}) \defs, line ->
when Str.split line " " is
when Str.splitOn line " " is
[":", name, .. as tokens, ";"] ->
ops = parseDef? tokens defs
Dict.insert defs name ops |> Ok
Expand Down
5 changes: 2 additions & 3 deletions exercises/practice/go-counting/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ parse = \boardStr ->

rows =
boardStr
|> Str.split "\n"
|> Str.toUtf8
|> List.splitOn '\n'
|> List.mapTry? \row ->
row
|> Str.toUtf8
|> List.mapTry \char ->
when char is
'B' -> Ok Black
Expand Down Expand Up @@ -125,4 +125,3 @@ territories = \boardStr ->
White -> { black: state.black, white: state.white |> Set.union newTerritory.territory, none: state.none }
None -> { black: state.black, white: state.white, none: state.none |> Set.union newTerritory.territory }
|> Ok

2 changes: 1 addition & 1 deletion exercises/practice/grep/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ grep = \pattern, flags, fileNames ->
findMatches : Config, Str, Str -> List { line : Str, index : U64 }
findMatches = \config, pattern, text ->
Str.split text "\n"
Str.splitOn text "\n"
|> List.mapWithIndex \line, index ->
{ line, index }
|> List.keepIf \{ line } ->
Expand Down
7 changes: 1 addition & 6 deletions exercises/practice/kindergarten-garden/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ studentIndex = \student ->
plants : Str, Student -> Result (List Plant) _
plants = \diagram, student ->
startIndex = 2 * studentIndex student
grid =
diagram
|> Str.trim
|> Str.split "\n"
|> List.map \row ->
row |> Str.trim |> Str.toUtf8
grid = diagram |> Str.toUtf8 |> List.splitOn '\n'
[(0, 0), (0, 1), (1, 0), (1, 1)]
|> List.mapTry \(row, column) ->
plant = grid |> List.get? row |> List.get? (startIndex + column)
Expand Down
5 changes: 2 additions & 3 deletions exercises/practice/matrix/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ parseRow : Str -> Result (List I64) [InvalidNumStr]
parseRow = \rowStr ->
rowStr
|> Str.trim
|> Str.split " "
|> Str.splitOn " "
|> List.map Str.trim
|> List.dropIf Str.isEmpty
|> List.mapTry Str.toI64

parseMatrix : Str -> Result (List (List I64)) [InvalidNumStr]
parseMatrix = \matrixStr ->
matrixStr
|> Str.trim
|> Str.split "\n"
|> Str.splitOn "\n"
|> List.mapTry parseRow

column : Str, U64 -> Result (List I64) [InvalidNumStr, OutOfBounds]
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/minesweeper/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ countNeighbors = \rows, x, y ->

annotate : Str -> Str
annotate = \minefield ->
rows = minefield |> Str.split "\n" |> List.map Str.toUtf8
rows = minefield |> Str.toUtf8 |> List.splitOn '\n'
annotated =
rows
|> List.mapWithIndex \row, y ->
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/ocr-numbers/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ convert = \grid ->
Ok ""
else

gridChars = grid |> Str.split "\n" |> List.map Str.toUtf8
gridChars = grid |> Str.toUtf8 |> List.splitOn '\n'
size = checkSize? gridChars
gridChars
|> List.chunksOf 4 # split vertically into groups of 4 rows
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/pig-latin/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pigLatinSwap = \chars ->
(_, 'y') if index > 0 -> Break (0, index) # rule 4
(_, c) if isVowel c -> Break (0, index) # rule 2
_ -> Continue (char, index + 1)
{ before, others } = chars |> List.split splitIndex
{ before, others } = chars |> List.splitAt splitIndex
others |> List.concat before

translateWord : Str -> Str
Expand All @@ -40,6 +40,6 @@ translateWord = \word ->
translate : Str -> Str
translate = \phrase ->
phrase
|> Str.split " "
|> Str.splitOn " "
|> List.map translateWord
|> Str.joinWith " "
2 changes: 1 addition & 1 deletion exercises/practice/poker/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bestHands = \hands ->

parseHand : Str -> Result Hand HandParsingError
parseHand = \handStr ->
cards = handStr |> Str.split " "
cards = handStr |> Str.splitOn " "
numCards = List.len cards
if numCards != 5 then
Err (InvalidNumberOfCards numCards)
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/rectangles/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rectangles : Str -> U64
rectangles = \diagram ->
grid =
diagram
|> Str.split "\n"
|> Str.splitOn "\n"
|> List.map Str.toUtf8
height = grid |> List.len
grid
Expand All @@ -40,4 +40,3 @@ rectangles = \diagram ->
|> List.sum
|> List.sum
|> List.sum

4 changes: 2 additions & 2 deletions exercises/practice/tournament/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ tally = \table ->
else

table
|> Str.split "\n"
|> Str.splitOn "\n"
|> List.mapTry? \row ->
when row |> Str.split ";" is
when row |> Str.splitOn ";" is
[team1, team2, resultStr] ->
result = resultStr |> parseResult?
Ok (team1, team2, result)
Expand Down
5 changes: 1 addition & 4 deletions exercises/practice/transpose/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module [transpose]
## Transpose the input string. Input string must be ASCII.
transpose : Str -> Str
transpose = \string ->
chars =
string
|> Str.split "\n"
|> List.map \row -> row |> Str.toUtf8
chars = string |> Str.toUtf8 |> List.splitOn '\n'
getChar = \row, col ->
chars |> List.get? row |> List.get col
maxWidth = chars |> List.map List.len |> List.max |> Result.withDefault 0
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/word-search/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ getChar = \grid, columnIndex, rowIndex ->

search : Str, List Str -> Dict Str WordLocation
search = \grid, wordsToSearchFor ->
{ rows, width } = grid |> Str.split "\n" |> List.map Str.toUtf8 |> padRight
{ rows, width } = grid |> Str.toUtf8 |> List.splitOn '\n' |> padRight
height = List.len rows
heightI64 = height |> Num.toI64
widthI64 = width |> Num.toI64
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/wordy/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ evaluateExpression = \accumulator, operations ->

answer : Str -> Result I64 [QuestionArgHadAnUnknownOperation Str, QuestionArgHadASyntaxError Str]
answer = \question ->
words = question |> Str.replaceEach "?" " ?" |> Str.split " "
words = question |> Str.replaceEach "?" " ?" |> Str.splitOn " "
when words is
["What", "is", numberString, .. as operations, "?"] ->
maybeStartNumber = Str.toI64 numberString
Expand All @@ -37,4 +37,3 @@ answer = \question ->
[_, "is", _, .., "?"] -> Err (QuestionArgHadAnUnknownOperation question)
[_, "are", .., "?"] -> Err (QuestionArgHadAnUnknownOperation question)
_ -> Err (QuestionArgHadASyntaxError question)

0 comments on commit 10300ce

Please sign in to comment.