You just need to tell Factor the expected stack effect.

See, this fails to compile, because the quotation is only selected at
run-time and the compiler isn't smart enough to examine both:

    : foo ( seq -- seq' )
        dup length even? [ 1 - ] [ 2 + ] ? map ;

But, you can always do something like this where you tell Factor to call
the quotation with the expected stack effect:

    : foo ( seq -- seq' )
        dup length even? [ 1 - ] [ 2 + ] ?
        '[ _ call( elt -- elt' ) ] map ;

I think that might incur some run-time performance penalties because it
would check the stack effect on every call, which doesn't matter for your
use-case, but might if it was inside a hot loop.

Best,
John.


On Fri, Feb 3, 2017 at 4:25 PM, Alexander Ilin <ajs...@yandex.ru> wrote:

> Hello!
>
>   I'm developing my chart gadget. I want to achieve this:
>
> ALIAS: x first
> ALIAS: y second
>
> : chart-axes ( chart -- seq )
>     [ dim>> ] [ axes>> ] bi [
>         nip
>     ] [
>         [ 0 swap 2array ] map
>     ] if* ;
>
>
> M: axis draw-gadget*
>     dup parent>> dup chart? [| axis chart |
>         axis vertical?>> :> vert?
>         chart dim>> :> dim
>         dim chart chart-axes vert? [ [ x ] bi@ ] [ [ y ] bi@ ] if
>         ! etc...
>     ] [ 2drop ] if ;
>
>   I thought I found a clever way to simplify that `if` there:
>
>         dim chart chart-axes vert? [ x ] [ y ] ? bi@
>
>   But the compiler says that I can't use `call` on runtime-computed
> quotations. Is there a way to work around that, like, by using a MACRO: or
> something?
>
>   Or is this a hard limitation on the available abstraction of the
> computation?
>
>   Basically, depending on a boolean flag I need to either take the first
> or the second element of the two arrays on the stack, and place the taken
> elements on the stack in the same order.
>
> ---=====---
>  Александр
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to