Cell

From Swym Wiki
Jump to: navigation, search

A cell is a convenient representation of an entry in an array. Cell is defined in Stdlyb as:

  'Cell' = struct
  {
    Int:'key'
    Array:'array'
  }

In other words, it's just an index, packaged together with an array to look it up in. Stdlyb also defines the following convenient functions for working with cells:

 Cell.'value' returns .array.at(.key)
 Cell.'value'('equals') returns .array.at(.key)=equals
 Cell.'nextCell' returns Cell.new(.key+1, .array)
 Cell.'previousCell' returns Cell.new(.key-1, .array)
 Array.'cells' returns array(.length) 'idx'->{ Cell.new(idx, this) }
 Cell.Array.'table' returns table[.each.key](.1st.array)
 Cell.Array.'cellKeys' returns [.each.key]
 Cell.Array.'cellValues' returns [.each.value]

Here's an example of how cells can be useful - generating an acronym!

First we find all the cells that contain a space (.cells.where{.value == " "}), and then for each one, read the value of the next cell (.each.nextCell.value) - in other words, each character that comes after a space.

"Now is the winter of our discontent".{[.1st, .cells.where{.value == " "}.each.nextCell.value]}
RunSandbox
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox