Skip to main content
added 229 characters in body
Source Link
cat
  • 6.1k
  • 2
  • 27
  • 45

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ 10 >base ] [ + ] map-reduce 10 base>
"." split [ 10 >base ] map 0 + reduce 10 base>
"." split [ 10 >base ] map sum 10 base>

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

Here's a place map-reduce is shorter:

[ "." split [ 10 base> ] [ [ 256 * ] dip + ] map-reduce ] bi@ - abs 1 + ]
[ "." split [ 10 base> ] map 0 [ [ 256 * ] dip + ] reduce ] bi@ - abs 1 + ]

Can't use sum there.

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ 10 >base ] [ + ] map-reduce 10 base>
"." split [ 10 >base ] map 0 + reduce 10 base>
"." split [ 10 >base ] map sum 10 base>

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ 10 >base ] [ + ] map-reduce 10 base>
"." split [ 10 >base ] map 0 + reduce 10 base>
"." split [ 10 >base ] map sum 10 base>

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

Here's a place map-reduce is shorter:

[ "." split [ 10 base> ] [ [ 256 * ] dip + ] map-reduce ] bi@ - abs 1 + ]
[ "." split [ 10 base> ] map 0 [ [ 256 * ] dip + ] reduce ] bi@ - abs 1 + ]

Can't use sum there.

deleted 15 characters in body
Source Link
cat
  • 6.1k
  • 2
  • 27
  • 45

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ string>number10 >base ] [ + ] map-reduce number>string10 base>
"." split [ string>number10 >base ] map 0 + reduce number>string10 base>
"." split [ string>number10 >base ] map sum number>string10 base>

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ string>number ] [ + ] map-reduce number>string
"." split [ string>number ] map 0 + reduce number>string
"." split [ string>number ] map sum number>string

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ 10 >base ] [ + ] map-reduce 10 base>
"." split [ 10 >base ] map 0 + reduce 10 base>
"." split [ 10 >base ] map sum 10 base>

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.

Source Link
cat
  • 6.1k
  • 2
  • 27
  • 45

find all the higher-order functions and use them, often

Factor is full of higher-order functions that take [ quotations ] or other-words and work on sequences / other data.

For example, to take an IP address as a string and sum its fields, you could do:

"." split [ string>number ] [ + ] map-reduce number>string
"." split [ string>number ] map 0 + reduce number>string
"." split [ string>number ] map sum number>string

Notice that fancy map-reduce is actually longest here because of [ quotation ] syntax, but there's already a sum word for that anyways, which is far shorter.