Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: passing a set of numbers to a function Date: Fri, 05 Feb 2016 13:43:57 +0000 Organization: A noiseless patient Spider Lines: 29 Message-ID: <8760y3mfvm.fsf@bsb.me.uk> References: <19679ed0-c594-4f7e-817c-a72bd5c10411@googlegroups.com> <22041181.SJtjrm8m6U@PointedEars.de> <87r3gso59h.fsf@bsb.me.uk> <1603158.xI32ru06Pi@PointedEars.de> <58b65993-5943-4f8e-a4e2-9e97bb1feea3@googlegroups.com> <1b6d9bd3-f349-476d-8d89-ab503011e056@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="7935"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jJ+mjpfPuuefXxPs6bB5LXg5tjmxv8wM=" Cancel-Lock: sha1:aU6erFJIUupO1U/NJqLEe7bKG7k= sha1:Y8K8Us/TfmF0sJrb5UunVNT19q4= X-BSB-Auth: 1.5b9045b4bb6e3c3d92b2.20160205134357GMT.8760y3mfvm.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:29535 Scott Sauyet writes: > I (Scott Sauyet) wrote: >> There is a simple way to implement mathematical sets, finite or >> infinite using predicate functions. [ ... ] >> >> const set = pred => ({ >> contains: pred, >> union: set2 => set(val => pred(val) || set2.contains(val)), >> intersection: set2 => set(val => pred(val) && set2.contains(val)), >> complement: () => set(val => !pred(val)) >> }); >> >> [ ... ] But, for one trying to use a set as a Collection, these >> are missing several important features. Obviously there is no way >> to add or remove members. [ ... ] > > This was perhaps hasty. Of course sets defined this way are > immutable, and hence one cannot add or remove members, but just as > we can create new sets from old in `union`, we can easily derive > new sets with `add` and `remove` functions: They can be made mutable by storing a predicate. Then they would like the mutable sets that most people are familiar with. But since I prefer a function style (as do you I think) I would not bother. -- Ben.