Monday, September 20, 2021

Calculator Python: Lambda Functions

Calculator Python: Lambda Functions


Introduction to Lambda Functions


Lambda functions are a quick, one expression, one line, python function.   Lambda functions do not require to be named though they can be named for future use if desired.  


The syntax for lambda functions are:


One argument:


lambda  argument : expression


Two or more arguments:


lambda  arg1, arg2, arg3, ...  : expression


The expression must return one result.  


The quick, versatile of lambda functions are make lambda functions one of the most popular programming tools.


Filter, Map, and Reduce


Filter:  uses a lambda function to filter out elements of a list and array using criteria.   For a list, the list command must be used to turn the result into an actual list. 

 

Syntax using Lambda and List:  


list(filter(lambda  arguments : expression))


Map:  uses a lambda function to apply a function to each element of a list.  Like filter, the list command must be used to turn the result into an actual list.


Syntax using Lambda and Map:


list(map(lambda arguments : expression))


Reduce:  uses a lambda function to use two or more arguments in a recursive function.


Syntax using Lambda:


reduce((lambda arguments : expressions), list)


Note, as of August 31, 2021, that the reduce command is NOT available on any calculator, only on full version of Python 3.  This may change with future updates.  


The following calculators have these commands in their Python programming (as of 8/30/2021):


HP Prime:  lambda, filter, map


Casio fx-CG 50 and fx-9750GIII: lambda, map


Numworks: lambda, filter, map


TI-84 Plus CE Python:  lambda, filter, map


TI-Nspire CX II Python:  lambda, filter, map


Nuwmorks Sample Python File: introlambda.py


from math import *

from random import *

# lambda test


n=randint(10,9999)

tens=lambda x:int(x/10)%10

hunds=lambda x:int(x/100)%10

thous=lambda x:int(x/1000)%10


print(n)

print(tens(n))

print(hunds(n))

print(thous(n))


print("List:")

l1=[1,2,3,4,5,6]

print(l1)


print("Filter demonstration")

print("Greater than 3")

l2=list(filter(lambda x:x>3,l1))

print(l2)

print("Odd numbers")

l3=list(filter(lambda x:x%2!=0,l1))

print(l3)


print("Map demonstration")

print("Triple the numbers")

l4=list(map(lambda x:3*x,l1))

print(l4)

print("exp(x)-1")

l5=list(map(lambda x:exp(x)-1,l1))

print(l5)


Sources


Maina, Susan "Lambda Functions with Practical Examples in Python"  Towards Data Science (membership blog with limited free access per month)  https://towardsdatascience.com/lambda-functions-with-practical-examples-in-python-45934f3653a8  Retrieved August 29, 2021


Simplilearn  "Learn Lambda in Python with Syntax and Examples"  April 28, 2021. https://www.simplilearn.com/tutorials/python-tutorial/lambda-in-python  Retrieved August 29, 2021


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Fun with the HP 30b

Fun with the HP 30 b Introduction The following programs are for the HP 30b Business Professional. Did you know that the 30b...