Origin Scripting Language
  • Basics
    • Syntax
    • Types
    • The Execution Loop
    • Defining Variables
      • Clone Objects (=)
      • References To Objects/Variables (@=)
    • Assignment Operators
    • Local Scoping
    • Typed Variables
  • Operators
    • Mathematical Usage
      • Addition Operator (+)
      • Subtraction Operator (-)
      • Divide Operator (/)
      • Multiply Operator (*)
      • To The Power Of (^)
      • Modulo Operator (%)
    • Text Usage
    • String Concatenation Operator (+)
    • Array Operations
    • Comparative Operators
    • Logical Operators
    • Bitwise operators
    • Pipe Operator (|>)
    • Nullish Coalescing Operator (??)
  • Program Flow
    • If Statements
      • if truthy (
      • ) else if truthy (
      • ) else (
      • )
    • Switch Case
    • Iteration
    • While And Until
    • Dynamic OSL Execution
  • Arrays And Objects
    • Making Arrays Or Objects
    • Modifying An Array
    • Clone Objects And References
    • Object Operations
    • Object Property Shorthand
  • Environment
    • The Window System
    • Mouse Cursor
    • Camera
    • Sound System
    • Input Simulation
    • Running Other Languages
    • Notifications
    • Send Data Between Windows
    • Interfacing With Rightclick
    • Permissions
    • Files
      • What is a file? (in ofsf)
      • Creating Directories
      • open("file_path")
      • fileGet(int)
      • listFiles()
  • Storage
    • Save DB
    • Local DB
  • Custom Syntax
    • Commands
    • Methods
    • Functions
    • Inline
    • Lambda
    • Spread Operator
    • Typed Parameters
    • Classes
    • Promises
  • External
    • Make An Iframe App
  • Commands
    • Debugging
      • log "hello world"
      • warn "you should change this"
      • error "something went wrong"
      • void expression
    • Rendering
      • Basics
        • Color Commands
        • Color Picker
        • Modifiers
      • Draw Cursor
        • goto x y
        • set_x x
        • set_y y
        • change_x x
        • change_y y
        • change x y
        • loc a b c d
      • Elements
        • Canvas
        • Rectangle
        • Triangle
        • Icon
        • Text
        • Image
        • Input
        • Pen
        • Hitbox
        • Bar
        • Slider
      • ICN (Icon System)
        • Drawing Commands
        • Dynamic Icons
      • Clipping And Scrolling (frames)
      • 3D Rendering
  • Functions
    • Math
      • Math()
      • random(low,high)
      • min(num1,num2)
      • max(num1,num2)
      • lcm(num1,num2)
      • gcd(num1,num2)
      • sum(num1,..)
      • average(num1,..)
      • dist(x1,y1,x2,y2)
      • degtorad(angle)
      • radtodeg(angle)
      • noise(x, y, z)
      • octaveNoise(x, y, z, octaves, persistence)
    • Types
      • typeof(value)
      • symbol(name)
    • Encoding
      • encodeURIComponent(string)
      • decodeURIComponent(string)
      • btoa/atob(string)
    • function()
    • formatFileSize(bytes)
    • getGamepads()
    • import(path)
    • ouidNew()
    • parseFilePath(path)
  • Global Variables
    • Overview
    • System Information
    • User Information
    • Date and Time
    • Input State
    • Display and UI
    • Network
    • File System
  • Methods
    • Strings
      • .padStart(num,val)
      • .padEnd(num,val)
      • .startsWith(val)
      • .endsWith(val)
      • .wrapText(characters)
      • .trimText(characters)
      • .split(characters)
      • .count(val)
      • .replace(old,new)
      • .replaceFirst(old,new)
      • .oslTokenise()
      • .strip()
      • .toOrdArray()
      • .match(pattern)
      • Encoding
        • .encodeBin()
        • .decodeBin()
        • .encodeHex()
        • .decodeHex()
        • .encodeUTF16()
        • .decodeUTF16()
        • .encrypt(password)
        • .decrypt(password)
        • .ord()
        • .chr()
      • Hashing
        • .hashMD5()
        • .hashSHA1()
        • .hashSHA256()
        • .hashSHA512()
      • Case
        • .toLower()
        • .toUpper()
        • .toMixed()
        • .toTitle()
    • Keyboard
      • .isKeyDown()
      • .onKeyDown()
    • Utilities
      • .len
      • .not
      • .ask()
      • .reverse()
      • .first()
      • .last()
      • .append(val)
      • .prepend(val)
      • .insert(location,val)
      • .delete(location)
      • .concat(val)
      • .trim(idx1,idx2)
      • .left(num)
      • .right(num)
      • .contains(val)
      • .index(val)
    • Maths
      • .abs()
      • .invabs()
      • .log()
      • .ln()
      • .isPrime()
      • .sqrt()
      • .sign()
      • .chance()
      • .clamp(low,high)
      • .toBase(base)
      • Rounding
        • .round(places)
        • .ceiling()
        • .floor()
      • Trigonometry
        • .sin
        • .cos
        • .tan
        • .asin()
        • .acos()
        • .atan()
        • .radSin()
        • .radCos()
    • Iframes
      • .iframeNew()
      • .iframeGoto()
      • .iframeResize()
      • .iframeRedirect()
      • .iframeShow()
      • .iframeHide()
      • .iframeClose()
    • JSON
      • Arrays
        • .pop()
        • .shift()
        • .randomOf()
        • .sort(direction)
        • .sortBy(key,direction)
        • .swap(idx1,idx2)
        • .fill(value)
        • .join(characters)
        • .map(callback)
        • .filter(callback)
        • .some(callback)
        • .every(callback)
      • Objects
        • .getKeys(keys)
        • .getValues()
      • .JsonStringify()
      • .JsonParse()
      • .JsonFormat()
      • .JsonClean()
    • Types
      • .getType()
      • .isType(type)
      • .toNum()
      • .toBool()
      • .toStr()
    • Functions
      • .bind(context)
    • Networking
      • .httpGet()
      • .http(method,data,headers)
      • .getAsync()
      • .roturConnect()
      • .roturSend(msg,target)
      • Websockets
        • .newWebsocket()
        • .wsSend(msg)
        • .wsClose()
        • .wsOpen()
        • .wsHasnew()
        • .wsGetnext()
        • .wsGetcount()
    • Timestamp
      • .timestamp("convert-date")
      • .timestamp("component")
Powered by GitBook
On this page
  • Syntax
  • Description
  • Important Constraints
  • Examples
  • Basic Usage
  • Chaining Multiple Operations
  • Working with Functions That Need Additional Parameters
  • Working with Arrays
  • Notes

Was this helpful?

Edit on GitHub
  1. Operators

Pipe Operator (|>)

The pipe operator (|>) in OSL allows you to chain function calls in a more readable way by passing the result of one expression as the input to another function. This creates a data pipeline that flows from left to right.

Syntax

expression |> function

This is equivalent to:

function(expression)

Description

The pipe operator takes the value on its left side and passes it as the first argument to the function on its right side. This allows you to create chains of operations that are more readable than nested function calls.

Key benefits of using the pipe operator:

  • Improves code readability by showing data flow from left to right

  • Reduces nesting of function calls

  • Makes complex transformations easier to understand

Important Constraints

In OSL, the pipe operator must be passed a function object directly, not a function call with parameters. This is because:

  1. The pipe operator expects a function object on its right side

  2. If you include parameters, the function would be executed first, and its return value (not the function itself) would be passed to the pipe

For example:

// CORRECT: Passing a function object
10 |> double

// INCORRECT: This would try to pass the result of double(3) to 10
10 |> double(3)  // This doesn't work as expected

Examples

Basic Usage

def double(number val) (
  return val * 2
)

// Without pipe operator
log double(10)  // Outputs: 20

// With pipe operator
log 10 |> double  // Outputs: 20

Chaining Multiple Operations

The pipe operator really shines when you need to apply multiple transformations in sequence:

def double(number val) (
  return val * 2
)

def addFive(number val) (
  return val + 5
)

def square(number val) (
  return val * val
)

// Without pipe operator (nested calls, read from inside out)
log square(addFive(double(10)))  // Outputs: 625

// With pipe operator (linear flow, read from left to right)
log 10 |> double |> addFive |> square  // Outputs: 625

Working with Functions That Need Additional Parameters

If you need to use a function that requires additional parameters with the pipe operator, you need to create a wrapper function:

def multiplyBy(number val, number factor) (
  return val * factor
)

// This won't work:
// 10 |> multiplyBy(3)

// Instead, create a wrapper function:
def multiplyByThree(number val) (
  return multiplyBy(val, 3)
)

log 10 |> multiplyByThree  // Outputs: 30

// Alternatively, use an anonymous function:
log 10 |> def(val) -> (
  return multiplyBy(val, 3)
)  // Outputs: 30

Working with Arrays

The pipe operator is particularly useful when working with array transformations:

numbers = [1, 2, 3, 4, 5]

def filterEven(array arr) (
  result = []
  for i arr.len (
    if arr[i] % 2 == 0 (
      result = result.append(arr[i])
    )
  )
  return result
)

def sum(array arr) (
  total = 0
  for i arr.len (
    total += arr[i]
  )
  return total
)

// For functions that need additional parameters, create wrapper functions
def multiplyAllByThree(array arr) (
  return arr.map(def(val) -> (
    return val * 3
  ))
)

// Without pipe operator
log sum(multiplyAllByThree(filterEven(numbers)))  // Outputs: 18

// With pipe operator
log numbers |> filterEven |> multiplyAllByThree |> sum  // Outputs: 18

Notes

  • The pipe operator passes the left value as the first argument to the function on the right

  • The function on the right must be a function object, not a function call with parameters

  • To use functions with additional parameters, create wrapper functions or use anonymous functions

  • The pipe operator has lower precedence than most other operators

  • You can chain multiple pipe operations: value |> func1 |> func2 |> func3

  • The pipe operator works with both named functions and anonymous functions

PreviousBitwise operatorsNextNullish Coalescing Operator (??)

Last updated 2 months ago

Was this helpful?