Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

functional code in php

32 views
Skip to first unread message

zermelo

unread,
Feb 10, 2012, 12:09:35 PM2/10/12
to
Hi,

I have to do a work in php, but I would like to write functional code as
much as possible. Anyone knows libraries/references where I can look for?

Thanks

ps I post here because perhaps some lisper has had my same problem and
has tested various strategies.

Alex Mizrahi

unread,
Feb 10, 2012, 1:44:42 PM2/10/12
to
> I have to do a work in php, but I would like to write functional code as
> much as possible.

Why? It doesn't make any sense.

And what is 'functional code'? Some people think that it is about
high-order functions, while others -- lack of side effects, immutable data.

It doesn't make any sense to write functional code if you don't know
what it is and why you need it.

> Anyone knows libraries/references where I can look for?

http://php.net/manual/en/functions.anonymous.php

namekuseijin

unread,
Feb 10, 2012, 1:59:47 PM2/10/12
to
On Feb 10, 4:44 pm, Alex Mizrahi <alex.mizr...@gmail.com> wrote:
> > I have to do a work in php, but I would like to write functional code as
> > much as possible.
>
> Why? It doesn't make any sense.
>
> And what is 'functional code'? Some people think that it is about
> high-order functions, while others -- lack of side effects, immutable data.
>
> It doesn't make any sense to write functional code if you don't know
> what it is and why you need it.

moreover, it doesn't make any sense to write in PHP specifically.
It's a dog of a language and barely holds together for ugly imperative
programming, let alone other paradigms...

> > Anyone knows libraries/references where I can look for?
>
> http://php.net/manual/en/functions.anonymous.php

just look at that!

class Cart
{
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;

protected $products = array();

public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}

public function getQuantity($product)
{
return isset($this->products[$product]) ? $this-
>products[$product] :
FALSE;
}

public function getTotal($tax)
{
$total = 0.00;

$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};

array_walk($this->products, $callback);
return round($total, 2);
}
}

it looks like the bastard son after some orgy full of perls, javas,
pythons and C... functional programming into that mix won't matter at
all...

zermelo

unread,
Feb 10, 2012, 3:38:43 PM2/10/12
to
On Fri, 10 Feb 2012 10:59:47 -0800, namekuseijin wrote:

> On Feb 10, 4:44 pm, Alex Mizrahi <alex.mizr...@gmail.com> wrote:
>> > I have to do a work in php, but I would like to write functional code
>> > as much as possible.
>>
>> Why? It doesn't make any sense.
>>
>> And what is 'functional code'? Some people think that it is about
>> high-order functions, while others -- lack of side effects, immutable
>> data.
>>
>> It doesn't make any sense to write functional code if you don't know
>> what it is and why you need it.
>

For functional code, I mean closures, map, filter, reduce and similar
Lisp-like functions. I have searched the official documentation, but I
don't see them. Maybe there exists some external library...any help?
I don't like php, but my boss wants it. So I was guessing whether someone
here had a remedy to alleviate the pain.

Pascal J. Bourguignon

unread,
Feb 10, 2012, 3:41:25 PM2/10/12
to
zermelo <zer...@invalid.spam> writes:

> I don't like php, but my boss wants it. So I was guessing whether someone
> here had a remedy to alleviate the pain.

PHP 5 even has closures, IIRC. So you can start to apply Greenspun Tenth
Law right now.

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

zermelo

unread,
Feb 10, 2012, 4:06:22 PM2/10/12
to
On Fri, 10 Feb 2012 21:41:25 +0100, Pascal J. Bourguignon wrote:

> zermelo <zer...@invalid.spam> writes:
>
>> I don't like php, but my boss wants it. So I was guessing whether
>> someone here had a remedy to alleviate the pain.
>
> PHP 5 even has closures, IIRC. So you can start to apply Greenspun
> Tenth Law right now.

Searching Greenspun's law on wikipedia, I discovered the corollary of
Robert Morris, who in turn was the creator of the first computer worm and
the partner of Paul Graham of viaweb, who is an ardent advocate of Lisp:)

Barry Margolin

unread,
Feb 10, 2012, 6:20:43 PM2/10/12
to
In article <jh3v8h$at3$1...@dont-email.me>, zermelo <zer...@invalid.spam>
wrote:

> On Fri, 10 Feb 2012 10:59:47 -0800, namekuseijin wrote:
>
> > On Feb 10, 4:44 pm, Alex Mizrahi <alex.mizr...@gmail.com> wrote:
> >> > I have to do a work in php, but I would like to write functional code
> >> > as much as possible.
> >>
> >> Why? It doesn't make any sense.
> >>
> >> And what is 'functional code'? Some people think that it is about
> >> high-order functions, while others -- lack of side effects, immutable
> >> data.
> >>
> >> It doesn't make any sense to write functional code if you don't know
> >> what it is and why you need it.
> >
>
> For functional code, I mean closures, map, filter, reduce and similar
> Lisp-like functions. I have searched the official documentation, but I
> don't see them. Maybe there exists some external library...any help?

PHP has all of those things.

lambda = create_function
mapcar = array_map
filter = array_filter
reduce = array_reduce
funcall = Assign funcname to variable, then $variable(<args>)

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

zermelo

unread,
Feb 11, 2012, 9:17:42 AM2/11/12
to
On Fri, 10 Feb 2012 18:20:43 -0500, Barry Margolin wrote:

> In article <jh3v8h$at3$1...@dont-email.me>, zermelo <zer...@invalid.spam>
> wrote:
>
>> On Fri, 10 Feb 2012 10:59:47 -0800, namekuseijin wrote:
>>
>> > On Feb 10, 4:44 pm, Alex Mizrahi <alex.mizr...@gmail.com> wrote:
>> >> > I have to do a work in php, but I would like to write functional
>> >> > code as much as possible.
>> >>
>> >> Why? It doesn't make any sense.
>> >>
>> >> And what is 'functional code'? Some people think that it is about
>> >> high-order functions, while others -- lack of side effects,
>> >> immutable data.
>> >>
>> >> It doesn't make any sense to write functional code if you don't know
>> >> what it is and why you need it.
>> >
>> >
>> For functional code, I mean closures, map, filter, reduce and similar
>> Lisp-like functions. I have searched the official documentation, but I
>> don't see them. Maybe there exists some external library...any help?
>
> PHP has all of those things.
>
> lambda = create_function
> mapcar = array_map
> filter = array_filter
> reduce = array_reduce
> funcall = Assign funcname to variable, then $variable(<args>)

Thanks.

M. Strobel

unread,
Feb 11, 2012, 11:35:33 AM2/11/12
to
I agree to the bastard son, but this should not keep you from writing good code.

All (small question mark here) of the necessary features are there.

You can write poems in any language.

/Str.

P.S. even with "defun"
0 new messages