# Reusable Functions: A simple lambda example with copious notes

**URL:** <https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888>\
**Category:** Tutorials & Examples\
**Tags:** lambda\
**Created:** [October 28, 2016, 6:42pm UTC](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888 "2016-10-28T18:42:09Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 5, 2017, 2:31am UTC](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888/11 "2017-03-05T02:31:20Z")

</div>

> [@papabrenta](#):
>
> The function named “log” expects an item name & will cast strings. s stands for the rule-supplied item.

Incorrect. The lambda expects an Item. Not an Item name (which would be a String, not of type GenericItem). It doesn’t cast anything. It calls the toString method on the State returned when calling the .state method on the passed in (I.e. s).

> [@papabrenta](#):
>
> s stands for the rule-supplied item.

Sorry of. Lambdas take 0 or more arguments. Reach argument is given a unique name. This name is exactly like a valid in your rules. So s is a val that contains the passed in Item.

> [@papabrenta](#):
>
> The function creates a string which is the state of a rule-supplied item (here, MyItem)

Yes, see my description above.

> [@papabrenta](#):
>
> That string is sent to the log file named lambda & will display in a terminal window by the command tail -f /var/log/openhab2/openhab.log.

Sort of. The string is sent to the logInfo action which is, as far as the rule is concerned is a function call. What happens from the depends on how logging is configured. By default the line will be written to openhab.log.

> [@papabrenta](#):
>
> According to a comment in the OLD syntax, the function’s last line, the string version of MyItem’s state + " logged" is returned to the rule that called the function & that created string populates val loggedStr in the rule.

That is true in both the old and new syntax.

[quote=“papabrenta, post:10, topic:15888”]  
I like logging not only the state, but also the name of the item. …  
This function could be a ready made method to call for logging items’ states . It also opens other possibilities for using functions to handle other often used methods.[/quote]

Using a lambda for something like the example above is not a very good idea. I chose this example because it was the easiest to understand that I could think of. In practice you would almost never want a lambda for something this trivial. See my [Separation of Behaviors](https://community.openhab.org/t/design-pattern-separation-of-behaviors/15886) for a better approach. And even then that use case is if you have complicated logic you want to apply (e.g. log to different places based on time of day). If all you are doing is logging out an item’s state it should be in line, not separate.

---

_[View the full topic](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888)._
