[Factor-talk] Important heads-up: Integers-as-sequences going away

2010-01-14 Thread Slava Pestov
Hi all,

In the next few days I'm going to merge some changes which eliminate
the overloading of integers as sequences. The rationale for this
change is that having integers be sequences is too error-prone. For
instance, if you accidentally write '{ 1 2 } 1 append' instead of
'{ 1 2 } 1 suffix' you'll end up creating an array with 10002
elements. It is easy to see how simple typos can lead to out of memory
errors, hangs, etc. This change will potentially require a lot of code
to be updated (I've spent most of the day updating core/ basis/ and
extra/ and I'm not done yet). However it is the first incompatible
change of such magnitude in quite some time, and there won't be any
others in the near future after this, giving users time to settle
down.

So to be precise, code such as the following will now be an error:

10 [ ... ] each

You should use iota now, which creates a new virtual sequence:

10 iota [ ... ] each

Note that 'iota' is already in Factor as of a few months ago, so you
can start updating code at any time.

If you want you can also use the integer-specific iteration
combinators in the math vocabulary (each-integer, all-integers?, etc).
These are also already present in the library, and have been for quite
some time. Note that thanks to the compiler's SCCP and escape analysis
optimizations, 'iota [ ... ] each' and '[ ... ] each-integer' compile
down to the exact same code, so don't pick one or the other based on
performance considerations. Most code should probably prefer to use
'each'; 'each-integer' only exists to implement 'each', after all.

The 'replicate' combinator which used to take a sequence (including an
integer) will now take integers only. That is, this will no longer
work:

hello [ 5 ] { } replicate-as . = { 5 5 5 5 5 }

Now you'll have to write something like

hello length [ 5 ] { } replicate-as . = { 5 5 5 5 5 }

This is because replicate is most often used with a length, not a
sequence of elements -- using it with a real sequence is not very
useful since it discards elements prior to calling the quotation.

As an aside, while updating library code I've noticed a few places
this idiom was used:

dup length [ ... ] 2each
dup length [ ... ] 2map

This no longer works, but for a while now we've had replacement
combinators which call the quotation, passing it an element together
with an index:

[ ... ] each-index
[ ... ] map-index

I'll post another message when the changes are merged. To clarify, I
haven't pushed anything yet, and in the latest binaries and GIT
sources, integers still supports sequence operations. Not for long,
though.

Slava

--
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] json vocabulary

2010-01-14 Thread Alec Berryman
Josh on 2010-01-13 23:57:54 -0600:

 The JSON vocab needs some love, I think:
 
 {1:2,3:4} json json

Your JSON is invalid.  The key value in a pair may only be a string.

 --- Data stack:
 {[0]:2,[0,1,2]:4}

I looked at the code.  jsvar-encode is the problem.  It doesn't seem
necessary - why should it matter if there's a dash in a string?  TR:
also seems to have integer-sequence problems; hopefully when Slava
removes them that'll be more apparent.

I guess the correct behavior in this case is to throw a error.  You
could quote the strings, but then json json wouldn't be a noop, and
that seems wrong.

It would probably also be easy to throw an error when the invalid input
is detected.  I would prefer an error early, but I could see the
argument for being liberal in what json accepts.

I'll put together a patch sometime soon.


--
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Important heads-up: Integers-as-sequences going away

2010-01-14 Thread Joe Groff
On Jan 14, 2010, at 4:53 AM, Slava Pestov wrote:

 The 'replicate' combinator which used to take a sequence (including an
 integer) will now take integers only. That is, this will no longer
 work:
 
 hello [ 5 ] { } replicate-as . = { 5 5 5 5 5 }
 
 Now you'll have to write something like
 
 hello length [ 5 ] { } replicate-as . = { 5 5 5 5 5 }

What do you think about times (each with a nullary quotation) also only 
taking integers? I bet it mostly gets used on integers too.

-Joe
--
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Important heads-up: Integers-as-sequences going away

2010-01-14 Thread Slava Pestov
On Fri, Jan 15, 2010 at 6:26 AM, Joe Groff arc...@gmail.com wrote:
 What do you think about times (each with a nullary quotation) also only 
 taking integers? I bet it mostly gets used on integers too.

This is already the case -- times is in the math vocabulary, and it
knows nothing about sequences:

: times ( n quot -- ) [ drop ] prepose each-integer ; inline

For anyone who wants to give these changes a shot before they get
merged, they're in the 'iota' branch. You'll need to 'make-image' from
a Factor instance built from 'master' first.

Slava

--
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] Not getting MACRO:

2010-01-14 Thread Jim mack
Hi.  Can you please help me understand why the final line here results in a
stack underflow?

USING: accessors arrays kernel locals present select8.html classes.tuple
sequences prettyprint fry macros nested-comments combinators ;

TUPLE: testing id firstname lastname age ;
: testing ( id fn ln age -- tpl ) testing boa ;
: testdata ( -- tpl ) 1 jim mack 46 testing ;
: testing-schema ( -- seq )
{   { First Name  [ firstname ] }
{ Last Name  [ lastname ] }
{ Age  [ age ] } } ;

: non-macro-way ( tpl -- )
{
[ First Name  write  firstname print ]
[ Last Name  write lastname print ]
[ Age  write  age present print ]
} cleave ;

testdata non-macro-way

! using fry

testdata { } clone
 testing-schema [ first2 '[ _ write  @ present print ] suffix ] each
  cleave

MACRO: easier ( alist -- ) { } clone swap
[ first2 '[ _ write  @ present print ] suffix ] each cleave ;

: fancyprint ( tpl -- )
testing-schema easier  ; inline

testdata fancyprint   ! stack underflow
--
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk