Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 184 total

LIMIT - Make sure val falls between lower and upper bounds, inclusive; uses length for series values.

   1  
   2      limit: func [
   3          "Make sure val falls between lower and upper bounds, inclusive; uses length for series values."
   4          val
   5          lower [integer!]
   6          upper [integer!]
   7          /show "For series values, show extension/truncation (dot/none for extension, 3 dots for truncation)."
   8          /local fill
   9      ][
  10          either not series? val [max min val upper lower] [
  11              either all [
  12                  upper >= length? val
  13                  lower <= length? val
  14              ] [val] [
  15                  ; If extending the series, use NONE as the fill value, so the
  16                  ; block can still be processed easily.
  17                  fill: either any-string? val ["."] [none]
  18                  head either lower >= length? val [
  19                      insert/only/dup tail val fill subtract lower length? val
  20                  ][
  21                      clear skip val upper
  22                      either show [change/dup skip tail val -3 '. 3] [val]
  23                  ]
  24              ]
  25          ]
  26      ]

sparse-rec-to-fixed-block

   1  
   2  	sparse-rec-to-fixed-block: func [
   3  		rec [block!]
   4  		col-names [block!] "All column names in full schema"
   5  	][
   6  		if not all-words? col-names [alert join "sparse-rec-to-fixed-block: col-names has non-word values: " mold col-names]
   7  		collect/only val [
   8  			foreach name col-names [
   9  				val: attempt [first select/skip rec name 2]
  10  			]
  11  		]
  12  	]
  13  

sparse-rec?

   1  
   2  	; Like named-fields?, this func can't guarantee that a record is really
   3  	; a sparse rec; it can only tell us if it's not.
   4  	sparse-rec?: func [
   5  		rec [block!]
   6  		col-names [block!] "All column names in full schema"
   7  	][
   8  		if not all-words? col-names [alert join "sparse-rec?: col-names has non-word values: " mold col-names]
   9  		all [
  10  			named-fields? rec
  11  			(length? rec) <> (2 * length? col-names) 
  12  		]
  13  	]

face-words - Returns a block of words that refer to faces in a face's pane.

   1  
   2      face-words: func [
   3          "Returns a block of words that refer to faces in the face's pane."
   4          face
   5      ][
   6          remove-each item collect word [foreach f face/pane [word: f/var]] [none? item]
   7      ]

named-fields?

   1  
   2  	; This can't guarantee that a record is actually a series of
   3  	; named fields; it can only tell if it *isn't*.
   4  	named-fields?: func [rec [block!]] [
   5  		parse rec [some [word! any-type!]]
   6  	]

SINGLE-LINE-MOLD

   1  
   2  	single-line-mold: func [
   3  	    "Reformats a block or object-spec to a single line."
   4  	    val [any-block! object!]
   5  	] [
   6  	    val: copy either any-block? val [val] [third val]
   7  	    replace/all mold new-line/all val  off  "^/" "^^/"
   8  	]

DATATYPE-NAME?

   1  
   2  datatype-name?: func [val [string!]] [datatype? get/any load val]

CAST

   1  
   2  cast: func [value type] [to type value]

CAN-CAST?

   1  
   2  can-cast?: func [value type] [not error? try [to type value]]]

ALL-WORDS? - returns true if all values in a block are word! values.

   1  
   2  all-words?: func [
   3  	"Returns true if all values in a block are word! values."
   4  	block [block!]
   5  ] [
   6  	parse block [some word!]
   7  ]
« Newer Snippets
Older Snippets »
Showing 11-20 of 184 total