Class Fn<T extends ACell>

Type Parameters:
T - Return type of function
All Implemented Interfaces:
IValidated, IWriteable, IFn<T>

public class Fn<T extends ACell> extends AClosure<T>
Value class representing a instantiated closure / lambda function. Includes the following information:
  1. Parameters of the function, as a vector of Syntax objects
  2. Body of the function, as a compiled operation
  3. captured lexical bindings at time of creation.
  • Method Details

    • create

      public static <T extends ACell, I> Fn<T> create(AVector<ACell> params, AOp<T> body)
    • withEnvironment

      public <F extends AClosure<T>> F withEnvironment(AVector<ACell> env)
      Description copied from class: AClosure
      Produces an copy of this closure with the specified environment
      Specified by:
      withEnvironment in class AClosure<T extends ACell>
      Parameters:
      env - New lexical environment to use for this closure
      Returns:
      Closure updated with new lexical environment
    • hasArity

      public boolean hasArity(int n)
      Description copied from class: AFn
      Tests if this function supports the given arity.
      Specified by:
      hasArity in class AFn<T extends ACell>
      Parameters:
      n - Arity to check
      Returns:
      true if function supports the given arity, false otherwise
    • invoke

      public Context<T> invoke(Context context, ACell[] args)
      Description copied from interface: IFn
      Invoke this function in the given context.
      Specified by:
      invoke in interface IFn<T extends ACell>
      Specified by:
      invoke in class AFn<T extends ACell>
      Parameters:
      context - Context in which the function is to be executed
      args - Arguments to the function
      Returns:
      Context containing result of function invocation, or an exceptional value
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      Specified by:
      isCanonical in class ACell
      Returns:
      true if the object is in canonical format, false otherwise
    • encode

      public int encode(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, excluding the tag byte.
      Specified by:
      encodeRaw in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures
      Returns:
      The estimated size for the binary representation of this object.
    • read

      public static <T extends ACell> Fn<T> read(ByteBuffer bb) throws BadFormatException
      Throws:
      BadFormatException
    • read

      public static <T extends ACell> Fn<T> read(Blob b, int pos) throws BadFormatException
      Throws:
      BadFormatException
    • print

      public boolean print(BlobBuilder sb, long limit)
      Description copied from class: AObject
      Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible.
      Specified by:
      print in class AObject
      Parameters:
      sb - BlobBuilder to append to. May be partially written if print limit exceeded
      limit - Limit of printing in string bytes
      Returns:
      True if fully printed within limit, false otherwise
    • printInternal

      public boolean printInternal(BlobBuilder sb, long limit)
      Description copied from class: AClosure
      Print the "internal" representation of a closure e.g. "[x] 1", excluding the 'fn' symbol.
      Specified by:
      printInternal in class AClosure<T extends ACell>
      Parameters:
      sb - StringBuilder to print to
      limit - Limit of BlobBuilder size
      Returns:
      True if printed successfully within limit, false otherwise
    • getParams

      public AVector<ACell> getParams()
      Returns the declared param names for a function.
      Returns:
      A binding vector describing the parameters for this function
    • getBody

      public AOp<T> getBody()
    • getRefCount

      public int getRefCount()
      Description copied from class: ACell
      Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. WARNING: may not be valid id Cell is not canonical Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell. WARNING: May be unreliable is cell is not canonical
      Overrides:
      getRef in class ACell
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • updateRefs

      public Fn<T> updateRefs(IRefFunction func)
      Description copied from class: ACell
      Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.
      Specified by:
      updateRefs in class AFn<T extends ACell>
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • toCanonical

      public ACell toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class ACell
      Returns:
      Canonical version of Cell