I gotta have my orange juice.

Jesu, Juva

A Lord’s Day Catechism

leave a comment »

What is today?

Today is the Lord’s day.

Why is this day special?

God gives us a day of rest as a gift; to remind us that Jesus is victorious risen Lord of all creation; and to give us hope that some day we will enjoy rest with him forever.

What do we do on the Lord’s day?

On the Lord’s day we love to worship God together as his people.

Why is it so good to worship God together?

Long ago, people feared to draw near to God. But because of Jesus’s death, we now enjoy God’s nearness at all times, and especially when his people gather together.

Written by Scott Moonen

June 3, 2006 at 11:41 am

Posted in Catechism

Tagged with , ,

A Mealtime Catechism

leave a comment »

Where did our food come from?

God provided for our food.

Why did God provide for our food?

God loves us and cares for us by providing for all of our needs.

How shall we thank God?

We thank God by our words, and by enjoying our food as a gift from him. Thank you, God!

Written by Scott Moonen

June 2, 2006 at 11:33 am

Posted in Catechism

Tagged with , ,

Common grace

with 3 comments

Introduction

There is a danger when engaged in any argument, even a just one, that we become so entranced with the rightness of our position and the wrongness of others, that we lose sight of our own unique tendencies to error. I believe the doctrines of grace to be true — I am, as Piper puts it, as 7-point Calvinist. But when we approach God’s sovereignty in a purely systematic or argumentative fashion, we can be at risk of forgetting things such as God’s love and kindness, or failing to allow God’s sovereignty to produce the worship and adoration that he deserves.

Perhaps this is a problem only for me. But I see in me a tendency to understand God’s sovereignty in such a way that his personality, emotion, and love are diminished in my mind, and this is not good. This particularly shows up in how I think of unbelievers. I believe God is sovereign in both election and reprobation. But if God predestines the reprobate to disobedience and destruction, can we honestly say that he truly loves them? After all, 1 Peter 2:7-8 says that:

for those who do not believe, “The stone that the builders rejected has become the cornerstone,” and “A stone of stumbling, and a rock of offense.” They stumble because they disobey the word, as they were destined to do.

Approaching Scripture with this kind of wooden philosophical presupposition can cause us to do damage to the text, to our understanding of God, and to our application of his word. With this in mind, I will briefly visit the topics of common grace, the love of God, and the cross; and touch on several related points in conclusion.

Common Grace

John Murray defines common grace as “every favour of whatever kind or degree, falling short of salvation, which this undeserving and sin-cursed world enjoys at the hand of God.” This is the sun and the rain that all the world enjoys in Matthew 5, which reads:

“You have heard that it was said, ‘You shall love your neighbor and hate your enemy.’ But I say to you, Love your enemies and pray for those who persecute you, so that you may be sons of your Father who is in heaven. For he makes his sun rise on the evil and on the good, and sends rain on the just and on the unjust. For if you love those who love you, what reward do you have? Do not even the tax collectors do the same? And if you greet only your brothers, what more are you doing than others? Do not even the Gentiles do the same? You therefore must be perfect, as your heavenly Father is perfect. — Matthew 5:43-48

The text commands us to love unbelievers in the same way that God does. But if we hold to the doctrines of grace in a wooden way, as a hyper-Calvinist, then we will be inclined to say that this common grace is not a mark of God’s love, since he plans to punish unbelievers in the end.

In fact, there is a frequent hyper-Calvinist argument (see, for example, [1]) that common grace, rather than being a token of God’s love and compassion, is nothing more than an entrapment by which God hardens unbeliever’s hearts and gradually builds up an overwhelming judicial case against them. Now this is true in one sense — God’s kindness is often lost on the unbeliever, and often serves to harden hearts. And certainly an unbelieving response to God’s kindness is storing up future judgment. But can we say that God has no love for the unbeliever?

The Lord is not slow to fulfill his promise as some count slowness, but is patient toward you, not wishing that any should perish, but that all should reach repentance. — 2 Peter 3:9

For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life. — John 3:16

And we have seen and testify that the Father has sent his Son to be the Savior of the world. — 1 John 4:14

Now it is true that not all the world is saved. But these and other verses speak powerfully of God’s heart, not just to mankind in general, but even to individual unbelievers specifically. It can be tempting to look at all the world through the lens of God’s eternal decrees, peering into questions whose answers are not given to us — who is saved and who is not? But then we will see God’s love only as it relates to salvation and destruction. We will be forced to interpret words like “world” in stilted ways, and we will miss an important aspect of God’s loving disposition toward the lost. God’s eternal decree of reprobation does not lessen his goodness, kindness, and love in giving gifts to unbelievers, individually, and to mankind in general. God is sovereign and God is holy, but he is also love; all of his attributes and perfections are in complete harmony.

Anyone who does not love does not know God, because God is love. — 1 John 4:8

Mark Horne writes:

Did God love Adam and Eve? Were His good gifts to them a revelation of His love for them, or were they snares meant to hurt them?

The answer must be that, though God foreordains whatsoever comes to pass, and ultimately causes all things, God’s gifts and offers of future reward are all genuine expressions of a genuine love. It may be difficult to conceive of how this objective revelation in history is to be reconciled with God’s eternal decrees, yet it is perverse to use the decrees to deny that God’s gifts and promises are motivated by love. The fact is, just as without God’s love there is no ground for God’s jealousy, so without God’s good gifts there is no ground for holding ingrates accountable for how they abuse and pervert these gifts. It was Satan’s strategy, after all, to deny that God loved Adam and Eve. If our inferences from God’s decrees put us in Satan’s camp, we need to rethink our position.

The Love of God

D. A. Carson addresses God’s love in his book, The Difficult Doctrine of the Love of God. This book is an outstanding treatment of God’s love and I highly recommend it. It is a short and accessible read. Carson distinguishes 5 major aspects of God’s love. This is certainly not an exhaustive list:

  1. God’s intra-Trinitarian love, between the members of the Godhead.
  2. God’s providential love for all his creation.
  3. God’s compassionate love toward fallen mankind.
  4. God’s particular, elective, saving, transforming love toward his people.
  5. Finally, Carson speaks of a sort of conditional love God has for his people, being careful to distinguish this from saving love. While our salvation is secure, our disobedience nonetheless grieves God and brings his fatherly discipline.

Carson is concerned that we embrace the full counsel of God concerning God’s love. Neglecting any one of these aspects of God’s love, or emphasizing some at the expense of others, will lead us into various errors. His chief concerns for us as reformed believers are, first, that we do not deny the reality of God’s love as a genuine and personal emotion; and second, that we do not deny the reality of God’s compassion for the lost.

The Cross

Calvinists hold to limited atonement, also definite atonement or particular redemption — the idea that Jesus’s death accomplished salvation for specific, elect individuals. We hold to particular redemption for several reasons. First, the benefits of Jesus’s atonement are frequently described in definite terms, suggesting that Jesus’s death was in itself wholly sufficient to accomplish and secure forgiveness, propitiation, righteousness, redemption, salvation; therefore his death cannot have been effective for those who perish in the same way that it is savingly effective for believers. Second, Jesus is often described as having died specifically for his people or his church. While he is also described as having died for the whole world, it is clear from this that believers benefit from his death in a way that the whole world does not. Lastly, and of less importance, John Owen made a famous reductio ad absurdum argument for the doctrine of particular redemption.

But again, if this doctrine is understood woodenly, we can fall into error. While Jesus’s death secured the atonement only of the elect, it is not true that Jesus did not die for all men. Again, it is wrong in many cases to artificially read “world” to mean only those who are saved. Charles Hodge writes of Jesus’s death:

In answer to this question, it may be remarked in the first place that Augustinians do not deny that Christ died for all men. What they deny is that he died equally, and with the same design, for all men. He died for all, that He might arrest the immediate execution of the penalty of the law upon the whole of our apostate race; that He might secure for men the innumnerable blessings attending their state on earth, which, in one important sense, is a state of probation; and that He might lay the foundation for the offer of pardon and reconciliation with God, on condition of faith and repentance.

These are the universally admitted consequences of his satisfaction, and therefore they all come within its design. By this dispensation it is rendered manifest to every intelligent mind in heaven and upon earth, and to the finally impenitent themselves, that the perdition of those that perish is their own fault. They will not come to Christ that they may have life. They refuse to have Him to reign over them. He calls but they will not answer. He says, “Him that cometh to me, I will in no wise cast out.” Every human being who does come is saved.

This is what is meant when it is said, or implied in Scripture, that Christ gave Himself as a propitiation, not for our sins only, but for the sins of the whole world. He was a propitiation effectually for the sins of his people, and sufficiently for the sins of the whole world. Augustinians have no need to wrest the Scriptures. They are under no necessity of departing from their fundamental principle that it is the duty of the theologian to subordinate his theories to the Bible, and teach not what seems to him to be true or reasonable, but simply what the Bible teaches. — Charles Hodge, ”Systematic”, vol, 2, pp. 558-9.

So while Jesus’s death does not secure reconciliation for the unbeliever, it nonetheless has a gracious effect for the unbeliever. Jesus’s death will in fact secure the redemption of creation itself. And as an expression of God’s love and grace, our holy God is able to restrain the sin of unbelievers and temporally withhold his judgment. Even in the face of sin and rebellion, he is righteously able to pour out great gifts of common grace upon unbelievers and mankind. And most importantly, Jesus’s death secures the offer of salvation to all men. He is a sure and certain savior, and no one who comes to him will be cast out (John 6:37). While the hyper-Calvinist may deny that God’s offer of salvation to the lost is “well-meant”, it is clear from scripture that God lovingly invites and commands all men to believe and be saved.

We see this clearly in passages that describe the work of the cross in expansive terms. Jesus is described as one who takes away the sin of the world (John 1:29); one who brought salvation for all people (Titus 2:11); the propitiation for the sins of the whole world (1 John 2:2); the savior of the world (1 John 4:14); and as the savior of all people, especially those who believe (1 Timothy 4:10). Other scripture would forbid us from affirming universalism, so it is clear from this that unbelievers are beneficiaries of the cross in some sense short of true salvation.

Conclusion

So we see that the balance of Scripture requires us to affirm both the doctrines of grace, but also God’s genuine love toward the lost. Carson writes:

In recent years I have tried to read both primary and secondary sources on the doctrine of the Atonement from Calvin on. One of my most forceful impressions is that the categories of the debate gradually shift with time so as to force disjunction where a slightly different bit of question-framing would allow synthesis.

He is saying that we can come to understand the doctrines of grace in such a wooden way that God’s love for unbelievers is diminished in our minds. We ought to guard against this.

I have in mind three other concerns for us as reformed believers that I don’t have time to address at length, but I do want to mention them briefly.

The first has to do with God’s love for the lost. If God has such a loving disposition to unbelievers, as his people we are to have the same love for unbelievers! This ought to energize our evangelism and service to the lost. When we invite unbelievers to salvation, we do not invite them on the condition that they are elect. We invite them unconditionally. In Redemption Accomplished and Applied, John Murray writes that:

From whatever angle we may view [the offer of the gospel], it is full, free, and unrestricted. The appeals of the gospel cover the whole range of divine prerogative and of human interest. God entreats, he invites, he commands, he calls, he presents the overture of mercy and grace, and he does this to all without distinction or discrimination. . . .

When Christ is presented to lost men in the proclamation of the gospel, it is as Savior he is presented, as one who ever continues to be the embodiment of the salvation he has once for all accomplished. It is not the possibility of salvation that is offered to lost men but the Saviour himself and therefore salvation full and perfect. There is no imperfection in the salvation offered and there is no restriction to its overture — it is full, free, and unrestricted. And this is the warrant of faith. (107ff)

In his lectures on the Marrow controversy, Sinclair Ferguson makes the point that God does not offer salvation to sinners as some abstract and conditional transaction — we believe, and from a distance, God will deal with our sin and dispense to us the benefits of the gospel like some kind of mail-order pharmacy. Rather, Jesus himself is offered as a personal and loving savior to all men. The benefits of the gospel come only as we are united with him.

The second concern has to do with God’s immutability. God never changes, and systematic theologians describe him as impassible, or without passions. But this does not mean that God is without emotion, or that his love is impersonal and mechanical. Carson writes that God’s impassibility is best understood as “trying to avoid a picture of a God who is changeable, given over to mood swings, dependent on his creatures” (49). God is not fickle or temperamental. Carson labors to communicate that God’s impassibility does not diminish the reality and personality of his emotions, including his love. Carson talks of people who would make God’s love to be nothing more than an anthropopathism, a crude human picture of something that is not really true of God. Carson makes this memorable appeal: “Give me a break. Paul did not pray that his readers might be able to grasp the height and depth and length and breadth of an anthropopathism and know this anthropopathism that surpasses knowledge (Eph. 3:14-21)” (59). God’s love is a real and perfect love!

The third concern is that we must progress from understanding and even experiencing God’s love to gratefulness and worship. How amazing it is that a holy God would love us! And how more amazing still as we discover more and more the greatness and depth of his love!

Bibliography

See also Monergism‘s collection of links on grace.

Written by Scott Moonen

March 3, 2006 at 5:33 pm

Global warming

leave a comment »

There is evidence that the earth’s climate has warmed slightly in recent history. It is uncertain whether this is part of a natural cycle, is directly attributable to the by-products of modern industry, or some combination of the two. Many individuals theorize that continued artificial production of greenhouse gases will have the direct catastrophic effect of measurable global warming, followed by melting of polar ice and a substantial rise in sea level.

There are several things that bother me with this simplistic analysis.

  1. The atmosphere is a complex and chaotic system. A single perturbation can have drastic and far-reaching results; El Nino demonstrates this. Global warming would almost certainly have unexpected consequences. If the current climate is in a metastable state, it is possible that global warming could cause it to metastasize to an antediluvian climate. But it is equally possible it could result in a so-called ice age.
  2. Warmer air holds more water vapor. Such water vapor could accelerate warming by acting as a greenhouse gas, or possibly moderate warming if manifested as cloud cover. Furthermore, evaporation of water would at least partially offset a rise in sea level.
  3. A rise in ocean temperature will reduce the solubility of carbon dioxide and other greenhouse gases, resulting in further release into the atmosphere. As with the increase in water vapor, this could result in an acceleration of warming.
  4. Ice cap melting produces freshwater. This increases the solubility of carbon dioxide and other greenhouse gases, at least partially offsetting the reduction in solubility due to temperature increase.

Most superficial popular analysis doesn’t address these points (although since first thinking about this, the movie The Day After Tomorrow portrayed the ice age angle). I’m sure these have been considered as part of scientific analysis, but that makes me wonder if there really is much cause for popular concern. I confess that my own grasp of this issue is only at a popular level.

Some links of interest:

Written by Scott Moonen

February 7, 2006 at 7:19 am

Salsa

leave a comment »

salsaThis salsa recipe is from a friend of ours at church. It makes a mild salsa that tastes very good.

Ingredients

  • 1 can petite diced tomatoes, drained
  • 1 tbl olive oil
  • 1 tbl vinegar
  • 1 tsp cumin
  • 1 tsp salt
  • 1 garlic clove, chopped
  • 1/4 c chopped onion

Mix ingredients and serve.

Written by Scott Moonen

January 16, 2006 at 7:04 am

Posted in Recipes

Tagged with , ,

Crock pot apple butter

leave a comment »

apple-butterThis recipe is from a friend of ours at church. I love apple butter, and we have made this recipe multiple times; it tastes great. I recommend making a double batch in a large crock pot.

Ingredients

  • 5 lbs. tart cooking apples, unpeeled, cored, cut up (enough to fill cooker).
  • 2 c. sugar
  • 1.25 tsp. ground cinnamon
  • 0.25 tsp. ground cloves
  • 0.25 tsp. ground nutmeg
  • 0.25 tsp. ground allspice
  • pinch of salt

Directions

  1. Coat slow cooker with non-stick spray. Fill with the apples, almost to the top; the exact amount is not critical. Sprinkle with sugar in layers while loading. Cover and let stand at room temperature all day; the apples will exude juice and collapse.
  2. Add spices and salt and toss the apples with a large wooden spoon. Cover and cook on low for 10 to 12 hours, or overnight.
  3. In the morning, remove the lid and let the apple butter cook an additional 2 to 8 hours on low to reach the desired thickness.
  4. Turn off the cooker and let the butter cool to room temperature in the crock pot. Transfer to a blender or food processor, or use a handheld immersion blender in the crock pot and purée the butter until completely smooth. Scrape with a rubber spatula into spring-top glass jars (or screw tops with new lids). Store, covered, in the refrigerator for up to 2 months or transfer to small plastic containers and freeze for up to 3 months.

Written by Scott Moonen

January 7, 2006 at 9:11 am

Joy at the End of the Tether

with one comment

tetherWilson, Douglas. Joy at the End of the Tether. Moscow, ID: Canon Press, 1999.

This book is a conversational walk through the book of Ecclesiastes. Douglas Wilson helped me to see for the first time the depth of wisdom and truth in Ecclesiastes. It is not exaggeration to say that Wilson has revolutionized my understanding of Ecclesiastes.

Previously I saw Ecclesiastes as portraying a world of emptiness and hopelessness, with an occasional disjointed glimmer of hope that there was some escape from the wasteland. But Wilson shows, conclusively I think, that Ecclesiastes is a unified whole. The world is full of vain repetition. But the message is not that we should become ascetics, forsaking the vain repetition of the world, for to do so rightly we would have to go out of the world! The message, rather, is that we should walk in faith, receive our lot as a gift from God, with appropriate joy and gratefulness. To the one who walks in unbelief, the vain repetition of this life brings nothing but despair. But to the one who walks in faith, trusting in God’s sovereignty and goodness, even the vain repetition of this life is a gift from God to be enjoyed.

This book has been tremendously helpful in encouraging me to walk in faith through difficulty and even tedium, challenging me to cultivate real gratefulness rather than a worldly gritty perseverance. This is part of faith’s growing in seeing all of life as being before the face of God (coram Deo).

I recommend this book very highly.

I’ve also encountered John Reisinger’s series titled “Thoughts on the Book of Ecclesiastes”. I don’t know much about Reisinger, nor have I yet done more than skim these articles. But Reisinger references Kaiser frequently, who was also one of Wilson’s primary sources. I’m retaining links to these articles for my reference; I don’t know yet whether I can recommend them: part 1, part 2, part 3, part 4, part 5, part 6, part 7, part 8.

Written by Scott Moonen

June 28, 2005 at 7:12 am

Redemption Accomplished and Applied

with one comment

murrayMurray, John. Redemption Accomplished and Applied. Grand Rapids, MI: William B. Eerdmans Publishing Co., 1984.

The late John Murray presents a brief overview of Jesus’s work of redemption. This book is divided into two parts: Redemption Accomplished, which describes our need of a savior, God’s provision of a savior, and what Jesus accomplished on the cross; and Redemption Applied, which describes how all of redemption is worked out in the life of the believer.

This book was a helpful overview of Jesus’s work on the cross and of God’s work in bringing me to salvation.

Of all of the chapters, the one that was most provocative to me was the chapter on faith and repentance. Murray presents a wonderful reminder of where our assurance of salvation is located — nowhere other than Jesus himself.

Following are this and some other quotes I’ve collected from the book.

On Assurance (pp. 107ff)

Murray reminds us that our assurance does not consist in peering into the secret decrees of God to discern whether he loves us or has elected us unto salvation (see also The Difficult Doctrine of the Love of God), nor does it consist of our subjective sense of nearness to God. It consists in placing our trust here and now wholly in Jesus for mercy:

What warrant does a lost sinner have to commit himself to Christ? How may he know that he will be accepted? How does he know that Christ is able to save? How does he know that this confidence is not misplaced? How does he know that Christ is willing to save him? . . .

From whatever angle we may view [the offer of the gospel], it is full, free, and unrestricted. The appeals of the gospel cover the whole range of divine prerogative and of human interest. God entreats, he invites, he commands, he calls, he presents the overture of mercy and grace, and he does this to all without distinction or discrimination. . . .

When Christ is presented to lost men in the proclamation of the gospel, it is as Savior he is presented, as one who ever continues to be the embodiment of the salvation he has once for all accomplished. It is not the possibility of salvation that is offered to lost men but the Saviour himself and therefore salvation full and perfect. There is no imperfection in the salvation offered and there is no restriction to its overture — it is full, free, and unrestricted. And this is the warrant of faith.

The faith of which we are now speaking is not the belief that we have been saved but [it is] trust in Christ in order that we may be saved. And it is of paramount concern to know that Christ is presented to all without distinction to the end that they may entrust themselves to him for salvation. The gospel offer is not restricted to the elect or even to those for whom Christ died. And the warrant of faith is not the conviction that we are elect or that we are among those for whom, strictly speaking, Christ died but [it is] the fact that Christ, in the glory of his person, in the perfection of his finished work, and in the efficacy of his exalted activity as King and Saviour, is presented to us in the full, free, and unrestricted overture of the gospel. It is not as persons convinced of our election nor as persons convinced that we are the special objects of God’s love that we commit ourselves to him but as lost sinners. We entrust ourselves to him not because we believe we have been saved but as lost sinners in order that we may be saved. It is to us in our lost condition that the warrant of faith is given and the warrant is not restricted or circumscribed in any way. In the warrant of faith the rich mercy of God is proffered to the lost and the promise of grace is certified by the veracity and faithfulness of God. This is the ground upon which a lost sinner may commit himself to Christ in full confidence that he will be saved. And no sinner to whom the gospel comes is excluded from the divine warrant for such confidence.

On Union With Christ (pp. 162-163)

Murray presents an excellent summary of what it means to be in union with Christ. He writes that “if we did not take account of [union with Christ], not only would our presentation of the application of redemption be defective but our view of the Christian life would be gravely distorted. Nothing is more central or basic than union and communion with Christ” (p. 161). He goes on to enumerate what it means to be united with Christ:

The fountain of salvation itself in the eternal election of the Father is “in Christ.” . . . The Father elected from eternity, but he elected in Christ. . . .

It is also because the people of God were in Christ when he gave his life a ransom and redeemed by his blood that salvation has been secured for them; they are represented as united to Christ in his death, resurrection, and exaltation to heaven. . . .

It is in Christ that the people of God are created anew. “We are his workmanship, created in Christ Jesus unto good works” (Eph. 2:10). . . .

But not only does the new life have its inception in Christ; it is also continued by virtue of the same relationship to him. It is in Christ that Christian life and behavior are conducted. . . .

It is in Christ that believers die. They have fallen asleep in Christ or through Christ and they are dead in Christ (1 Thess. 4:14, 16). . . .

Finally, it is in Christ that the people of God will be resurrected and glorified. It is in Christ they will be made alive when the last trumpet will sound and the dead will be raised incorruptible (1 Cor. 15:22). It is with Christ they will be glorified (Rom. 8:17).

Written by Scott Moonen

June 20, 2005 at 3:52 pm

Small is the Trust When Love is Green

leave a comment »

Small is the trust when love is green
In sap of early years;
A little thing steps in between
And kisses turn to tears.

Awhile – and see how love be grown
In loveliness and power!
Awhile, it loves the sweets alone,
But next it loves the sour.

A little love is none at all
That wanders or that fears;
A hearty love dwells still at call
To kisses or to tears.

Such then be mine, my love to give,
And such be yours to take:-
A faith to hold, a life to live,
For lovingkindness’ sake:

Should you be sad, should you be gay,
Or should you prove unkind,
A love to hold the growing way
And keep the helping mind:-

A love to turn the laugh on care
When wrinkled care appears,
And, with an equal will, to share
Your losses and your tears.

–Robert Louis Stevenson

Written by Scott Moonen

June 13, 2005 at 5:37 am

Functional Python

with 3 comments

A friend of mine is learning Python and was curious about functional programming, so I have written this brief tutorial. Python isn’t a full-fledged functional language, but it supports some very useful functional idioms.

It’s best to approach this tutorial by programming along at the Python interactive prompt. Try typing everything in to see what the results are.

Introduction

Imagine you have a list of numbers and want to filter out all even numbers:

q = [1,2,5,6,8,12,15,17,20,23,24]

def is_even(x) :
  return x % 2 == 0

result = filter(is_even, q)

(Remember that the “%” operator is the modulus or remainder operator. If the remainder when a number is divided by two is zero, then the number must be even.)

If we only use is_even once, it’s kind of annoying that we have to define it. Wouldn’t it be nice if we could just define it inside of the call to filter? We want to do something like “q = filter(x % 2 == 0, q)“, but that won’t quite work, since the “x % 2 == 0” is evaluated before the call to filter. We want to pass a function to filter, not an expression.

We can do this using the lambda operator. lambda allows you to create an unnamed throw-away function. Try this:

q = [1,2,5,6,8,12,15,17,20,23,24]
result = filter(lambda x : x % 2 == 0, q)

lambda tells Python that a function follows. Before the colon (:) you list the parameters of the function (in this case, only one, x). After the colon you list what the function returns. This function doesn’t have a name, and Python disposes of it as soon as filter is done with it.

You can, however, assign the function to a variable to give it a name. The following two statements are identical:

def is_odd(x) :
  return x % 2 == 1
 
is_odd = lambda x : x % 2 == 1

map

Here’s another example. The map function applys a function to every item in a list, and returns the result. This example adds 1 to every element in the list:

result = map(lambda x : x + 1, [1,2,3,4,5])

(At this point, result holds [2,3,4,5,6].)

The map function can also process more than one list at a time, provided they are the same length. This allows you to do things like add all of the elements in a pair of lists. Note that our lambda here has two parameters:

result = map(lambda x,y : x+y, [1,2,3,4,5], [6,7,8,9,10])

(At this point, result holds [7, 9, 11, 13, 15].)

reduce

Python also provides the reduce function. This is a bit more complicated; you provide it a list and a function, and it reduces that list by applying the function to pairs of elements in the list. An example is the best way to understand this. Let’s say we want to find the sum of all the elements in a list:

sum = reduce(lambda x,y : x+y, [1,2,3,4,5])

reduce will first apply the function to 1,2, yielding 3. It will then apply the function to 3,3 (the first 3 is the result of adding 1+2), yielding 6. It will then apply the function to 6,4 (the 6 is the result of adding 3+3), yielding 10. Finally, it will apply the function to 10,5, yielding 15. The result stored in sum is 15.

Similarly, we can find the cumulative product of all items in a list. The following example stores 120 (=1*2*3*4*5) in product:

product = reduce(lambda x,y : x*y, [1,2,3,4,5])

Conditionals

Beginning in Python 2.5 you can even express conditions within your lambdas, using Python’s conditional expressions. So, for example:

reciprocals = map(lambda x : 1.0/x if x !=  0 else None, [0, 1, 2, 3, 4, 5])

At this point, reciprocals holds the list [None, 1.0, 0.5, 0.333..., 0.25, 0.2]. The expression “1/x if x != 0 else None” is the conditional expression, and it is used to prevent division by zero. If x is not 0, then the result is 1/x, but otherwise it is None.

Background

One of the advantages of lambda-functions is that they allow you to write very concise code. A result of this, however, is that the code is dense with meaning, and it can be hard to read if you are not accustomed to functional programming. In our first example, filter(is_even, q) was fairly easy to understand (fortunately, we chose a descriptive function name), while filter(lambda x : x % 2 == 0, q) takes a little longer to comprehend.

If you are a masochistic mathematical geek, you’ll probably enjoy this (I do). If not, you might still prefer to break things into smaller pieces by defining all of your functions first and then using them by name. That’s ok! Functional programming isn’t for everyone.

Another advantage of functional programming is that it corresponds very closely to the mathematical notion of functions. Note that our lambda-functions didn’t store any results into variables, or have any other sort of side effect. Functions without side effects cause fewer bugs, because their behavior is deterministic (this is related to the dictum that you aren’t supposed to use global variables). It is also much easier to mathematically prove that such functions do what you think they are doing. (Note that it’s still possible to write a lambda function that has side effects, if the lambda function calls another function that has side effects; for example lambda x : sys.stdout.write(str(x)) has the side effect of printing its parameter to the screen.)

Written by Scott Moonen

June 9, 2005 at 7:13 am