Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32212

Re: Functional Programming needs some real world use cases

Newsgroups comp.lang.javascript
Date 2017-01-19 17:26 -0800
References <8b2dc802-a0d2-4ae0-ba75-98ecf5989e64@googlegroups.com> <87bmv2erpx.fsf@bsb.me.uk> <6b8d9806-b427-4b74-b74f-b582cf1ceba1@googlegroups.com> <a8be0d39-6f93-4051-9fc9-1827446a02d0@googlegroups.com> <dd1317b4-8b1e-4594-bb85-6834f47d477c@googlegroups.com>
Message-ID <2904795a-c86f-4a52-be28-e4f7bb8efbfe@googlegroups.com> (permalink)
Subject Re: Functional Programming needs some real world use cases
From justaguy <lichunshen84@gmail.com>

Show all headers | View raw


On Thursday, January 19, 2017 at 4:01:31 PM UTC-5, justaguy wrote:
> On Thursday, January 19, 2017 at 3:41:27 PM UTC-5, justaguy wrote:
> > On Thursday, January 19, 2017 at 3:35:09 PM UTC-5, justaguy wrote:
> > > On Thursday, January 19, 2017 at 3:10:25 PM UTC-5, Ben Bacarisse wrote:
> > > > justaguy writes:
> > > > 
> > > > > I'm starting to read about Functional Programming.  The following is
> > > > > some sample code, which seems concise.  In the meantime, I wonder if
> > > > > you have some idea of using other "action" other than "print", and of
> > > > > course, feel free to use other array "values" where appropriate.
> > > > >
> > > > > function forEach(array, action) {
> > > > >   for (var i = 0; i < array.length; i++)
> > > > >     action(array[i]);
> > > > > }
> > > > >
> > > > > forEach(["Wampeter", "Foma", "Granfalloon"], print);
> > > > 
> > > > As an exercise, what expression can you write in place of "..." to print
> > > > all seven strings (and just those):
> > > > 
> > > >   forEach([["a","b","c"],["q","r"],["z"]], ...);
> > > > 
> > > > -- 
> > > > Ben.
> > > 
> > > console.log
> > > 
> > > you have three arrays inside an array
> > 
> > console.log is another action, that does not solve your problem. Yours seems tricky.
> 
> 
> function forEach(array, action) { 
>   for (var i = 0; i < array.length; i++) 
> 	// if (Array.isArray(i)) {
> 	if ( i === Array ) {
> 		 for (var i2 = 0; i2 < i.length; i2++)
> 		 action(i[i2]); 
> 		}	 
> 	else {
> 		action(array[i]); 
> 	}
> } 
> 
> I'm a bit puzzled, the above code executes the else portion for 
> [["a","b","c"],["q","r"],["z"]] data
> 
> Probably we should stick with the original code but use another "action", > but what's that?  Thanks.

function forEach(array, action) { 
  for (var i = 0; i < array.length; i++) 
	action(array[i]); 
	// if (Array.isArray(i)) {
	/*
	if ( i === Array ) {
		 for (var i2 = 0; i2 < i.length; i2++)
		 action(i[i2]); 
		}	 
	else {
		action(array[i]); 
	}
	*/
	} 
	
	var recurse = function (i) {
	if ( i === Array ) {
			 for (var i2 = 0; i2 < i.length; i2++)
			 // print(i[i2]); 
			 // console.log('element is array:' + i[i2]);
			 return document.getElementById('output').innerHTML += 'element is array:' + i[i2] + ',';
			}	 
		else {
			// print([i]); 
			return document.getElementById('output').innerHTML += [i] + ',';
		}	
	}	
	

caller = forEach([["a","b","c"],["q","r"],["z"]], recurse);

it puts a, b, c, q, r, z on the page.  Hmm, I'll see if I can manage to get the browser print function in as well later ...
 

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-18 20:09 -0800
  Re: Functional Programming needs some real world use cases Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-19 20:10 +0000
    Re: Functional Programming needs some real world use cases Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-19 21:16 +0100
      Re: Functional Programming needs some real world use cases Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-19 21:24 +0000
    Re: Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-19 12:34 -0800
      Re: Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-19 12:41 -0800
        Re: Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-19 13:01 -0800
          Re: Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-19 17:26 -0800
    Re: Functional Programming needs some real world use cases Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-19 21:23 +0000
      Re: Functional Programming needs some real world use cases Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-19 21:50 +0000
    Re: [Firefox]"print" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-24 01:52 +0100
      Re: [Firefox]"print" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-24 05:45 +0100
  Re: Functional Programming needs some real world use cases Scott Sauyet <scott@sauyet.com> - 2017-01-20 04:28 +0000
    Re: Functional Programming needs some real world use cases justaguy <lichunshen84@gmail.com> - 2017-01-19 21:18 -0800
    Re: Functional Programming needs some real world use cases John Harris <niam@jghnorth.org.uk.invalid> - 2017-01-20 10:22 +0000
      Re: Functional Programming needs some real world use cases Scott Sauyet <scott@sauyet.com> - 2017-01-21 01:29 +0000
    Re: Functional Programming needs some real world use cases Scott Sauyet <scott@sauyet.com> - 2017-01-21 01:15 +0000
    Re: Functional Programming needs some real world use cases "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-21 15:00 -0800
  Re: Functional Programming needs some real world use cases Scott Sauyet <scott@sauyet.com> - 2017-01-20 05:00 +0000
  Re: Functional Programming needs some real world use cases Scott Sauyet <scott@sauyet.com> - 2017-01-20 05:02 +0000

csiph-web