Skip to content

Commit

Permalink
feat: create modular range functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Mar 30, 2024
1 parent c7b85b0 commit 1eb7d42
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 359 deletions.
29 changes: 25 additions & 4 deletions prelude/__internal__/Byte.mad
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { Maybe } from "Maybe"

import List from "List"
import {} from "Scan"



#iftarget js

import { Just, Nothing } from "Maybe"



_scanByte :: (Byte -> Maybe Byte) -> Maybe Byte -> String -> Maybe Byte
_scanByte = (just, nothing, str) => (#- {
_scanByte = (just, nothing, str) => (
#- {
const n = parseInt(str)
return isNaN(n) ? nothing : just(n)
} -#)
} -#
)

scanByte = _scanByte(Just, Nothing)

Expand All @@ -25,6 +33,21 @@ instance Scan Byte {
scan = scanByte
}

/**
* Return a list of bytes within the given range. The start
* parameter is inclusive and the end parameter is exclusive.
*
* @since 0.23.1
* @example
* range(3, 3) // []
* range(3, 5) // [3, 4]
*/
range :: Byte -> Byte -> List Byte
export range = (start, end) => {
s = toInteger(start)
return List.repeatWith((i) => fromInteger(i + s), toInteger(end) - s)
}

#iftarget js

toInteger :: Byte -> Integer
Expand Down Expand Up @@ -68,5 +91,3 @@ fromFloat :: Float -> Byte
export fromFloat = extern "madlib__number__floatToByte"

#endif


17 changes: 17 additions & 0 deletions prelude/__internal__/Float.mad
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Maybe } from "Maybe"

import List from "List"
import {} from "Scan"


Expand Down Expand Up @@ -30,6 +32,21 @@ instance Scan Float {
scan = scanFloat
}

/**
* Return a list of floats within the given range. The start
* parameter is inclusive and the end parameter is exclusive.
*
* @since 0.23.1
* @example
* range(3, 3) // []
* range(3, 5) // [3, 4]
*/
range :: Float -> Float -> List Float
export range = (start, end) => {
s = toInteger(start)
return List.repeatWith((i) => fromInteger(i + s), toInteger(end) - s)
}

#iftarget js

fromInteger :: Integer -> Float
Expand Down
3 changes: 2 additions & 1 deletion prelude/__internal__/IO.mad
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Wish } from "Wish"
import type { Error } from "__IOError__"

import { equals, when } from "Function"
import Integer from "Integer"
import { mapWithIndex } from "List"
import List from "List"
import { cShow, pShow } from "Show"
Expand Down Expand Up @@ -428,5 +429,5 @@ export printColorPalette = () => List.forEach(
putLine("")
}
},
List.range(0, 256),
Integer.range(0, 256),
)
14 changes: 14 additions & 0 deletions prelude/__internal__/Integer.mad
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Maybe } from "Maybe"

import List from "List"
import {} from "Scan"


Expand Down Expand Up @@ -30,6 +32,18 @@ instance Scan Integer {
scan = scanInteger
}

/**
* Return a list of integers within the given range. The start
* parameter is inclusive and the end parameter is exclusive.
*
* @since 0.23.1
* @example
* range(3, 3) // []
* range(3, 5) // [3, 4]
*/
range :: Integer -> Integer -> List Integer
export range = (start, end) => List.repeatWith((i) => i + start, end - start)

#iftarget js

toFloat :: Integer -> Float
Expand Down
11 changes: 0 additions & 11 deletions prelude/__internal__/List.mad
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,6 @@ export repeatWith = (f, count) => {
}


/**
* Return a list of integers within the given range. The start
* parameter is inclusive and the end parameter is exclusive.
*
* @since 0.12.0
* @example
* range(3, 3) // []
* range(3, 5) // [3, 4]
*/
range :: Integer -> Integer -> List Integer
export range = (start, end) => repeatWith((i) => i + start, end - start)


/**
Expand Down
7 changes: 4 additions & 3 deletions prelude/__internal__/Network.mad
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Maybe } from "Maybe"

import Integer from "Integer"
import List from "List"
import Math from "Math"
import { Just, Nothing } from "Maybe"
import Short from "Short"
import String from "String"
import Math from "Math"



Expand Down Expand Up @@ -160,7 +161,7 @@ export getIpAddressesInRange = (start, end) => {
return pipe(
ipV4ToInteger,
Math.add(1),
List.range(startInt),
map(ipV4FromInteger)
Integer.range(startInt),
map(ipV4FromInteger),
)(end)
}
3 changes: 2 additions & 1 deletion prelude/__internal__/Random.spec.mad
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Float from "Float"
import Integer from "Integer"
import List from "List"
import Math from "Math"
import String from "String"
Expand Down Expand Up @@ -108,7 +109,7 @@ test(
r1 = generate(num)
r2 = generate(num)
multiType = (rand) => {
numbers = shuffle(List.range(0, 10), rand)
numbers = shuffle(Integer.range(0, 10), rand)
strings = shuffle(ALPHABET, rand)
return #[numbers, strings]
}
Expand Down
7 changes: 5 additions & 2 deletions prelude/__internal__/Random/LCG.mad
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export mkSeed = pipe(
stringToInt :: String -> Integer
export stringToInt = pipe(
ByteArray.fromString,
ByteArray.reduce((hash, byte) => hash + (Integer.fromByte(byte) << 6) + (Integer.fromByte(byte) << 16), 0),
ByteArray.reduce(
(hash, byte) => hash + (Integer.fromByte(byte) << 6) + (Integer.fromByte(byte) << 16),
0,
),
)

mkSeedFromString :: String -> Seed
Expand Down Expand Up @@ -181,7 +184,7 @@ export next = perturb(LCG_C)
*/
accumulate :: (a -> a) -> Integer -> a -> a
export accumulate = (fn, steps, initial) => pipe(
List.range(0),
Integer.range(0),
List.reduce((agg, _) => fn(agg), initial),
)(steps)

Expand Down
18 changes: 18 additions & 0 deletions prelude/__internal__/Short.mad
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Maybe } from "Maybe"

import List from "List"
import {} from "Scan"


Expand Down Expand Up @@ -31,6 +33,22 @@ instance Scan Short {
}


/**
* Return a list of shorts within the given range. The start
* parameter is inclusive and the end parameter is exclusive.
*
* @since 0.23.1
* @example
* range(3, 3) // []
* range(3, 5) // [3, 4]
*/
range :: Short -> Short -> List Short
export range = (start, end) => {
s = toInteger(start)
return List.repeatWith((i) => fromInteger(i + s), toInteger(end) - s)
}


#iftarget js

toInteger :: Short -> Integer
Expand Down
Loading

0 comments on commit 1eb7d42

Please sign in to comment.