Skip to main content
added syntax-highlighting
Source Link
Deduplicator
  • 9.3k
  • 5
  • 34
  • 53

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 
someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if condition while avoiding cluttering the global/package namespace with small throw-away labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without cluttering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 
#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if condition while avoiding cluttering the global/package namespace with small throw-away labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without cluttering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if condition while avoiding cluttering the global/package namespace with small throw-away labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without cluttering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 
spellfixes. made clear that the if condition was refered
Source Link

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the ifif condition while avoiding cluteringcluttering the global/package namespace with small throwawaythrow-away labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without cluteringcluttering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if while avoiding clutering the global/package namespace with small throwaway labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without clutering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if condition while avoiding cluttering the global/package namespace with small throw-away labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without cluttering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 
added 92 characters in body
Source Link
crasic
  • 109
  • 7

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if while avoiding clutering the global/package namespace with small throwaway labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without clutering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if while avoiding clutering the global/package namespace with small throwaway labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without clutering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 

If the language supports it, I usually use labeled anonymous functions to accomplish this.

someCondition = lambda p: True if [complicated expression involving p] else False #I explicitly write the function with a ternary to make it clear this is a a predicate if (someCondition(p)): #do stuff... 

IMHO this is a good compromise because it gives you the readability benefit of not having the complicated expression cluttering the if while avoiding clutering the global/package namespace with small throwaway labels. It has the added benefit that the function "definition" is right where its being used making it easy to modify and read the definition.

It doesn't have to only be predicate functions. I like to encase repeated boiler-plate in small functions like this as well (It works particularly well for pythonic list generation without clutering the bracket syntax). For example, the following oversimplified example when working with PIL in python

#goal - I have a list of PIL Image objects and I want them all as grayscale (uint8) numpy arrays im_2_arr = lambda im: array(im.convert('L')) arr_list = [im_2_arr(image) for image in image_list] 
Source Link
crasic
  • 109
  • 7
Loading