Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created August 11, 2017 14:36
Show Gist options
  • Save zgohr/7e42c38c99e9ad68a13721ad787bf130 to your computer and use it in GitHub Desktop.
Save zgohr/7e42c38c99e9ad68a13721ad787bf130 to your computer and use it in GitHub Desktop.
Elm simplification questions from Slack
addBreadcrumb : Crumb -> List Crumb -> List Crumb
addBreadcrumb breadcrumb breadcrumbs =
let
cutBcrumbs bcrumbs newBcrumbs =
case bcrumbs of
Just crumbs ->
case List.head crumbs of
Just crumb ->
if breadcrumb /= crumb then
cutBcrumbs (List.tail crumbs) (List.append newBcrumbs [ crumb ])
else
List.append newBcrumbs [ crumb ]
Nothing ->
newBcrumbs
Nothing ->
newBcrumbs
in
if List.any (\bc -> breadcrumb == bc) breadcrumbs then
cutBcrumbs (Just breadcrumbs) []
else
List.append breadcrumbs [ breadcrumb ]
addBreadcrumb : Crumb -> List Crumb -> List Crumb
addBreadcrumb crumb crumbs =
case crumbs of
[] ->
[ crumb ]
x :: xs ->
if x == crumb then
[ crumb ]
else
x :: addBreadcrumb crumb xs
queryfn =
let
elementMaybe =
Array.get index theQuery.selectlinescontainer
element =
case elementMaybe of
Just el ->
el
Nothing ->
xSelectLine
newElement =
{ element | equation = input }
newContainer =
Array.set index newElement theQuery.selectlinescontainer
newQueryEl =
{ theQuery | selectlinescontainer = newContainer }
index =
theQuery.queryId
beforePart =
if (index > 0) then
List.take index model.queries
else
[]
afterPart =
List.drop (index + 1) model.queries
in
{ model | queries = beforePart ++ [ newQueryEl ] ++ afterPart } ! []
-- The first 7 lines, for example, could be this: `element = Array.get index theQuery.selectlinescontainer |> Maybe.withDefault xSelectLine`
-- The next few could be `newQueryEl = { theQuery | selectlinescontainer = Array.set theQuery.queryId { element | equation = input } theQuery.selectlinescontainer }`
-- and the next bit could be `queries = List.indexedMap (\idx -> if idx == theQuery.queryId then (always newQueryEl) else identity) model.queries`
-- now, with those in hand, you could look into spltting some of that off into helper functions, gradually moving your logic out of your `update` until it just dispatches functions, that call helpers to do their thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment