Prelude of builtin functions #7
No reviewers
Labels
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: probablycorey/shrimp#7
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "prelude"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Changes in this branch:
Language Features
Conditionals: Added ConditionalOp statements at statement-level (eg
do x: x > 2 end)Operators:
%(modulo operator)==for equalityor/andchaining behaviorIdentifiers: Allowed
?character in identifier names (e.g.,empty?,valid?)Module System
loadkeyword: Import/use modules (evolved fromusekeyword)Builtin Functions
Prelude system: Built-in global functions available in all Shrimp programs:
Shrimp Prelude Functions
Output/Display
echo- Print values to consoleType Introspection
type- Get the type of a valueinspect- Format a value for displaylength- Get the length of a string, array, or dictType Predicates
string?- Check if value is a stringnumber?- Check if value is a numberboolean?- Check if value is a booleanarray?- Check if value is an arraydict?- Check if value is a dictfunction?- Check if value is a function or native functionnull?- Check if value is nullsome?- Check if value is not nullBoolean/Logic
not- Logical negationUtilities
inc- Increment a number by 1dec- Decrement a number by 1identity- Return the input value unchangedCollection Operations
at- Get element at index/key from array or dictrange- Create a number sequenceempty?- Check if string, array, or dict is emptyString Operations
Transformations
str.join- Join array elements into stringstr.split- Split string by separatorstr.to-upper- Convert to uppercasestr.to-lower- Convert to lowercasestr.trim- Remove leading/trailing whitespacestr.replace- Replace first occurrencestr.replace-all- Replace all occurrencesstr.slice- Extract substring by indicesstr.substring- Extract substring by indices (alt)str.repeat- Repeat string n timesstr.pad-start- Pad string at startstr.pad-end- Pad string at endstr.lines- Split string into linesstr.chars- Split string into charactersString Predicates
str.starts-with?- Check if starts with prefixstr.ends-with?- Check if ends with suffixstr.contains?- Check if contains substringstr.empty?- Check if string is emptystr.test?- Test string against regexString Search
str.index-of- Find first index of substringstr.last-index-of- Find last index of substringstr.match- Match string against regexList Operations
Core Operations
list.map- Transform each elementlist.filter- Keep elements matching predicatelist.reduce- Reduce to single valuelist.find- Find first matching elementlist.slice- Extract subarrayList Predicates
list.empty?- Check if list is emptylist.contains?- Check if list contains itemlist.any?- Check if any element matcheslist.all?- Check if all elements matchSequence Operations
list.reverse- Reverse list orderlist.sort- Sort listlist.concat- Concatenate multiple listslist.flatten- Flatten nested listslist.unique- Remove duplicateslist.zip- Combine two lists into pairsList Access
list.first- Get first elementlist.last- Get last elementlist.rest- Get all but first elementlist.take- Take first n elementslist.drop- Drop first n elementslist.append- Add element to endlist.prepend- Add element to startlist.index-of- Find index of elementList Utilities
list.sum- Sum all numberslist.count- Count elements matching predicatelist.partition- Split into matching/non-matchinglist.compact- Remove null valueslist.group-by- Group elements by key functionDict Operations
Core Operations
dict.keys- Get all keysdict.values- Get all valuesdict.entries- Get key-value pairs as objectsdict.get- Get value by key with defaultdict.merge- Merge multiple dictsdict.map- Transform valuesdict.filter- Keep entries matching predicatedict.from-entries- Create dict from array of pairsDict Predicates
dict.has?- Check if key existsdict.empty?- Check if dict is emptyMath Operations
Basic Math
math.abs- Absolute valuemath.floor- Round downmath.ceil- Round upmath.round- Round to nearestmath.min- Minimum valuemath.max- Maximum valuemath.pow- Exponentiationmath.sqrt- Square rootmath.random- Random number [0, 1)math.clamp- Restrict value to rangemath.sign- Sign of number (-1, 0, 1)math.trunc- Truncate decimalMath Predicates
math.even?- Check if evenmath.odd?- Check if oddmath.positive?- Check if positivemath.negative?- Check if negativemath.zero?- Check if zeroEnumerables
each- Iterate over list, return original listModules
load- Load and execute Shrimp module from file05cf93ab36to4f092fca3f4f092fca3ftode30d85304de30d85304to0eca3685f5WIP: start on a prelude of builtin functionsto Prelude of builtin functionsLove it
@ -0,0 +5,4 @@]echo bobecho (mike | at name)Is this an example or just a tmp file.
Whoopsie. Just a tmp file.
@ -17,6 +18,7 @@ const operators: Array<Operator> = [{ str: '-', tokenName: 'Minus' },{ str: '>', tokenName: 'Gt' },{ str: '<', tokenName: 'Lt' },{ str: '%', tokenName: 'Modulo' },LOL, I was thinking about this on the drive home from school yesterday.
@ -0,0 +3,4 @@import { Compiler } from '#compiler/compiler'import { type Value, VM, Scope } from 'reefvm'export const load = async function (this: VM, path: string): Promise<Record<string, Value>> {Why use
thishere instead ofvm?@ -110,3 +109,3 @@if (value instanceof RegExp) value = String(value)if (value === expected) {if (isEqual(value, expected)) {I rewrote this in the branch I'm on too!
If you replace this line with
You get to lean on bun's type checking code and you get all the nice diff support.
Awesome. Done.
@ -59,2 +38,2 @@assertNever(result)}if (result.type === 'function') return Functionelse return fromValue(result)🧠