Skip to content

Commit

Permalink
fix: use foldr instead of foldl
Browse files Browse the repository at this point in the history
  • Loading branch information
linsyking committed May 9, 2023
1 parent 13dca7d commit 24016e6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "linsyking/messenger-core",
"summary": "Core library for Messenger.",
"license": "BSD-3-Clause",
"version": "3.0.0",
"version": "3.0.1",
"exposed-modules": {
"Definitions": [
"Messenger.Recursion",
Expand Down
17 changes: 9 additions & 8 deletions src/Messenger/RecursionArray.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Array implementation for the recursion algorithm
-}

import Array exposing (Array)
import Array.Extra exposing (insertAt)
import Messenger.Recursion exposing (RecBody)


Expand All @@ -32,7 +33,7 @@ updateObjects rec env msg objs =

updateOnce : RecBody a b c d -> c -> b -> Array a -> ( Array a, ( List ( d, b ), List b ), c )
updateOnce rec env msg objs =
Array.foldl
Array.foldr
(\ele ( lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv ) ->
let
( newObjs, newMsg, newEnv ) =
Expand All @@ -52,7 +53,7 @@ updateOnce rec env msg objs =
unfinishedMsg =
List.filter (\( x, _ ) -> not (rec.super x)) newMsg
in
( Array.push newObjs lastObjs, ( lastMsgUnfinished ++ unfinishedMsg, lastMsgFinished ++ finishedMsg ), newEnv )
( insertAt 0 newObjs lastObjs, ( unfinishedMsg ++ lastMsgUnfinished, finishedMsg ++ lastMsgFinished ), newEnv )
)
( Array.empty, ( [], [] ), env )
objs
Expand All @@ -72,7 +73,7 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
else
let
( newObjs, ( newUnfinishedMsg, newFinishedMsg ), newEnv ) =
Array.foldl
Array.foldr
(\ele ( lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv ) ->
let
msgMatched =
Expand All @@ -88,14 +89,14 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
in
if List.isEmpty msgMatched then
-- No need to update
( Array.push ele lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv )
( insertAt 0 ele lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv )

else
-- Need update
let
-- Update the object with all messages in msgMatched
( newObj, ( newMsgUnfinished, newMsgFinished ), newEnv2 ) =
List.foldl
List.foldr
(\msg ( lastObj2, ( lastMsgUnfinished2, lastMsgFinished2 ), lastEnv2 ) ->
let
( newEle, newMsgs, newEnv3 ) =
Expand All @@ -115,14 +116,14 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
unfinishedMsgs =
List.filter (\( x, _ ) -> not (rec.super x)) newMsgs
in
( newEle, ( lastMsgUnfinished2 ++ unfinishedMsgs, lastMsgFinished2 ++ finishedMsgs ), newEnv3 )
( newEle, ( unfinishedMsgs ++ lastMsgUnfinished2, finishedMsgs ++ lastMsgFinished2 ), newEnv3 )
)
( ele, ( [], [] ), env )
msgMatched
in
( Array.push newObj lastObjs, ( lastMsgUnfinished ++ newMsgUnfinished, lastMsgFinished ++ newMsgFinished ), newEnv2 )
( insertAt 0 newObj lastObjs, ( newMsgUnfinished ++ lastMsgUnfinished, newMsgFinished ++ lastMsgFinished ), newEnv2 )
)
( Array.empty, ( [], [] ), env )
objs
in
updateRemain rec newEnv ( newUnfinishedMsg, finishedMsg ++ newFinishedMsg ) newObjs
updateRemain rec newEnv ( newUnfinishedMsg, newFinishedMsg ++ finishedMsg ) newObjs
16 changes: 8 additions & 8 deletions src/Messenger/RecursionList.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ updateObjects rec env msg objs =

updateOnce : RecBody a b c d -> c -> b -> List a -> ( List a, ( List ( d, b ), List b ), c )
updateOnce rec env msg objs =
List.foldl
List.foldr
(\ele ( lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv ) ->
let
( newObj, newMsg, newEnv ) =
Expand All @@ -51,7 +51,7 @@ updateOnce rec env msg objs =
unfinishedMsg =
List.filter (\( x, _ ) -> not (rec.super x)) newMsg
in
( lastObjs ++ [ newObj ], ( lastMsgUnfinished ++ unfinishedMsg, lastMsgFinished ++ finishedMsg ), newEnv )
( newObj :: lastObjs, ( unfinishedMsg ++ lastMsgUnfinished, finishedMsg ++ lastMsgFinished ), newEnv )
)
( [], ( [], [] ), env )
objs
Expand All @@ -71,7 +71,7 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
else
let
( newObjs, ( newUnfinishedMsg, newFinishedMsg ), newEnv ) =
List.foldl
List.foldr
(\ele ( lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv ) ->
let
msgMatched =
Expand All @@ -87,14 +87,14 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
in
if List.isEmpty msgMatched then
-- No need to update
( lastObjs ++ [ ele ], ( lastMsgUnfinished, lastMsgFinished ), lastEnv )
( ele :: lastObjs, ( lastMsgUnfinished, lastMsgFinished ), lastEnv )

else
-- Need update
let
-- Update the object with all messages in msgMatched
( newObj, ( newMsgUnfinished, newMsgFinished ), newEnv2 ) =
List.foldl
List.foldr
(\msg ( lastObj2, ( lastMsgUnfinished2, lastMsgFinished2 ), lastEnv2 ) ->
let
( newEle, newMsgs, newEnv3 ) =
Expand All @@ -114,14 +114,14 @@ updateRemain rec env ( unfinishedMsg, finishedMsg ) objs =
unfinishedMsgs =
List.filter (\( x, _ ) -> not (rec.super x)) newMsgs
in
( newEle, ( lastMsgUnfinished2 ++ unfinishedMsgs, lastMsgFinished2 ++ finishedMsgs ), newEnv3 )
( newEle, ( unfinishedMsgs ++ lastMsgUnfinished2, finishedMsgs ++ lastMsgFinished2 ), newEnv3 )
)
( ele, ( [], [] ), env )
msgMatched
in
( lastObjs ++ [ newObj ], ( lastMsgUnfinished ++ newMsgUnfinished, lastMsgFinished ++ newMsgFinished ), newEnv2 )
( newObj :: lastObjs, ( newMsgUnfinished ++ lastMsgUnfinished, newMsgFinished ++ lastMsgFinished ), newEnv2 )
)
( [], ( [], [] ), env )
objs
in
updateRemain rec newEnv ( newUnfinishedMsg, finishedMsg ++ newFinishedMsg ) newObjs
updateRemain rec newEnv ( newUnfinishedMsg, newFinishedMsg ++ finishedMsg ) newObjs

0 comments on commit 24016e6

Please sign in to comment.