Git Product home page Git Product logo

Comments (19)

vygr avatar vygr commented on May 6, 2024

Well that’s interesting but not for the reason you might think :)

(push list val val val) is what you should be doing.

(push 1 x y z) should fail with an error as 1 is not a list !

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Second. Find with a string only cares (currently) about finding the first char of the find string in a string or finding the object you gave in the list. The exact same object, as in a symbol.

No eql test is being done so doing

(find “abc” ‘(“abc” “xyz”))

Is most likely to do somthing undefined. But I will look into this and improve matters for you over the weekend.

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

You can do:

(defun-bind find-str (s l)
    (some (lambda (_s)
        (if (eql s _s) _)) l))

For now to get you going. ie return the index of the first eql test in the list, else nil.

In (some) (each) (every) (not-every) (map) (reduce) (filter).... the _ symbol is bound to the index value for the lambda or function that's used for the forms function call. So you can make use of that.

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

And raising an issue here on anomalies is fine BTW.

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Trying that here I get.

ChrysaLisp Terminal 1.5

lisp
ChrysaLisp 1.3
Press ESC/Enter to exit.
(push 1 '(1 2 3))
Error: (push array form ...) wrong_types ! < (1 (1 2 3)) > File: stdin(1)

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

(push 1 '(2 3))
Error: (push array form ...) wrong_types ! < (1 (2 3)) > File: stdin(4)

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Also it's worth pointing out that:

(push '(1 2) 3)

Would end up with the list '(1 2 3) BUT and it's a big but to remember, that '(1 2) is a static list ! It's a quoted list that was read in by the (read) call !

(push) mutates the list in place, does not create a new list. ! If you want a new list do:

(cat '(1 2) '(3)) or (cat '(1 2) (list 3)) etc

Now this can be useful, that mutation of a static list, if that's what you want to do ! For example:

(defun-bind circle (r)
    ;cached circle generation
    (defq i (% (logior r) 7)
        k (elem i '(()()()()()()()))
        p (elem i '(()()()()()()())))
        (cond
               (defq i (some (lambda (i) (if (= i r) _)) k))
                      (elem i p))
                (t
                     (push k r)
                     (elem -2 (push p (list (path-gen-arc 0.0 0.0 0.0 (const fp_2pi) r 0.25 (path))))))))

This has 2 static lists in the function, as a flat map, and caches the polygon representing the circles of radius r, and returns that polygon....

from chrysalisp.

FrankC01 avatar FrankC01 commented on May 6, 2024

Chris

My typo was putting the element that I was pushing as the first arg. While it was a typo my point was more around some lisp semantics of a list has push adding the element to the beginning of the list as if it were a cons or the cat example you provided.

For what I am doing with it, cat will work just fine (cat '(1) '(2 3))

As far as the (find ...) using some is fine and get's me where I needed which is detecting if "-h" is in the argument list from stdio. It is a built in argument setup automagically for what I am doing.

Next question is around reduce. I was very accustomed to using, in Clojure, the (reduced...) function that would stop a reduction at any point and return the argument provided in the reduced call. Thoughts?

Cheers

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

So (reduced) was used as a kind of return statement inside the (reduce) lambda ?

I’m not familiar with Closure (or some would argue, even Lisp ;) )

So was there some constraint on where you could place the (reduced) ? Could it be deep down the call stack of the function doing the reduction ?

You may have noticed that ChrysaLisp has no (return)

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

If so that sounds like a (catch) mechanism being used to achieve the result ?

from chrysalisp.

FrankC01 avatar FrankC01 commented on May 6, 2024

There is an option to 'seed' the accumulator
Contrived example to put it in a Chrys context...

(defun reducer-to-five (acc el)
	(if (= el 5)
		(reduced acc)
		(push acc el)))

(defun up-to-five (alist)
	; (reduce form accumulator sequence)
	(reduce reducer-to-five alist (list)))

So I'm not familiar with the catch throw behavior quite yet so I'm not sure but do you mean:

(defun reducer-to-five (acc el)
	(if (= el 5)
		(throw acc)
		(push acc el)))

(defun up-to-five (alist)
	; (reduce form accumulator sequence)
	(catch (reduce reducer-to-five alist (list))))

So (up-to-five '(0 1 5 7 9))
Will return (0 1) ?

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Yes that’s the kind of thing I was thinking. That the implementation, under the hood, was likely to be some use of a catch/throw.

What might be useful then would be a function wrapper/macro that lets a function use (return ...) ?

I would rather avoid the situation where all functions have to allow for this return catch mechanism. Opt in by saying:

(defun-returns......)

Or something like that ?

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Btw, pushed the start of improvements to the (find) and (find-rev). First step being to promote the concept to be part of the seq class interface.

Now I can add the option to use an eql test if required in certain combinations of elem and seq.

Again I don’t want to reduce the performance of the case where (find) is used to fast search a symbol flat map or char in a string. These cases are critical performance for the parser, case statements and method call dispatch.

from chrysalisp.

FrankC01 avatar FrankC01 commented on May 6, 2024

Let me give catch or throw a whirl for the reduced. In a language I created the reduced function imbued a reduced type that carried the user provided form and in the reduction loop I was checking the return type for each invocation of the lambda. If detected I would break out of the iteration and return the form on the reduced type

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

I have thought of an issue with the (catch)(throw) approach for this. Even thought that idea would work, it will only work in the debug builds ! In the release build ALL the machinery for (throw)ing and any exception and error code is stripped from the boot image. This save about 30-35KB and brings more performance with it, but it does mean that I've assumed that no code that uses (catch) will ever happen in release mode !

So this will need further thought.

May be that one has to roll your own (reduce-with-reduced) function using (some!) or (each!). The current (reduce) is just a function provided in boot.inc, so it wouldn't be a big issue to provide a variant that allowed early exit and no use of (catch).

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Maybe...

(defun-bind reduce-reduced (_f _l &optional _a)
	;(reduce-reduced lambda seq [accum]) -> form
	(defq _x nil)
	(cond
		(_a (some! 0 -1 t (lambda (_p)
				(cond (_x t) (t (setq _a (_f _a _p)) nil))) (list _l)))
		(t (setq _a (elem 0 _l))
			(some! 1 -1 t (lambda (_p)
				(cond (_x t) (t (setq _a (_f _a _p)) nil))) (list _l)))) _a)

(defmacro-bind reduced (_a)
	;(reduced accum)
	`(progn (setq _x t) ,_a))

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

Pushed proposed additions to boot.inc with slight renaming from above.

from chrysalisp.

vygr avatar vygr commented on May 6, 2024

@FrankC01 are we done here ;)

from chrysalisp.

FrankC01 avatar FrankC01 commented on May 6, 2024

Indeed, closing the thread

from chrysalisp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.