~jojo/Carth

2ffd86d155a095c6a5293cc68fe7ac02daed235e — JoJo 1 year, 23 days ago b144030
Fix partitionWith reversing lists. Caused def order issue in genInit

genInit must genDefineGlobVar:s in topological order, but an earlier
call to partitionWith had accidentally reversed the topological order,
causing this to break:

    (define (main r) (bar r))
    (define bar bar1)
    (define bar1 foo)
    (define (foo r) [Unit r])
M examples/fizzbuzz.carth => examples/fizzbuzz.carth +1 -1
@@ 1,6 1,6 @@
(import std)

(define (main real-world) (fizzbuzz real-world))
(define main fizzbuzz)

(define fizzbuzz
  (io/for (range 1 100)

M src/Misc.hs => src/Misc.hs +2 -3
@@ 6,7 6,6 @@ import Data.List (intercalate)
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Maybe
import Data.Foldable
import Control.Monad.Reader
import Control.Monad.Writer
import Control.Monad.State


@@ 88,8 87,8 @@ splitOn sep = fromMaybe [] . Mega.parseMaybe splitOn'
        pure $ (as ++) $ if not (null a) then [a] else []

partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
partitionWith f = foldl'
    (\(bs, cs) x -> case f x of
partitionWith f = foldr
    (\x (bs, cs) -> case f x of
        Left b -> (b : bs, cs)
        Right c -> (bs, c : cs)
    )

M test/tests/good/fibonacci.carth => test/tests/good/fibonacci.carth +2 -2
@@ 2,8 2,8 @@

(import std)

(define (main real-world)
  (display (show-int (fib 92)) real-world))
(define main
  (display (show-int (fib 92))))

(define fib
  (define (fib' a b n)

M test/tests/good/sieve-of-eratosthenes.carth => test/tests/good/sieve-of-eratosthenes.carth +2 -2
@@ 2,9 2,9 @@

(import std)

(define (main real-world)
(define main
  (let1 primes (sieve (range-from 2))
    (display (show-int (unwrap! (iter/nth (cast 1000) primes))) real-world)))
    (display (show-int (unwrap! (iter/nth (cast 1000) primes))))))

(define (sieve xs)
  (Iter (fun (_)