April 18, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Graham Fawcett | On Thursday, 18 April 2013 at 13:04:19 UTC, Graham Fawcett wrote:
> On Sunday, 14 April 2013 at 23:52:37 UTC, Idan Arye wrote:
>>
>> Now, I've got it working(the example actually compiles and prints "less then 5"), and I want to make a pull request to put it in Phobos(because it's a useful function) but I don't really know where to put it. None of the existing modules seems fit, and I don't want to open a new module(std.monad?) for a single function.
>
> Nit-picky observation: there's nothing monadic about your predSwitch function, it wouldn't belong in a 'std.monad' module.
>
> Best,
> Graham
A monad is "a structure that represents computations"(quote from Wikipedia). While the Turing machine branch(=the main branch) of computability theory quickly fell into the Turing tarpit and had to moved to pseudo-code, the Lambda calculus was expended using the traditional mathematical method of defining symbols that replace other symbols.
That's why when in the Turing machine branch you could just write `if ... then ... else ...` and it's okay because it's pseudo-code, in Lambda calculus you actually have an IFTHENELSE defined as a lambda expression. And so on with loops, exception handling, and even type definitions and input/output.
I find that `predSwitch` - a function that represents the `switch` idiom - perfectly fits into the definition of monads.
|
April 18, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On Thursday, 18 April 2013 at 13:58:46 UTC, Idan Arye wrote:
> Naturally - even as statements, a `switch` statement without a switch expression is no better than a chain of `if`-`else`s.
>
As mentioned before in other thread, LLVM is crazy good at transforming if/else chains into switch statement, and GCC does a good job at it as well.
|
April 18, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Thursday, 18 April 2013 at 14:26:08 UTC, deadalnix wrote:
> On Thursday, 18 April 2013 at 13:58:46 UTC, Idan Arye wrote:
>> Naturally - even as statements, a `switch` statement without a switch expression is no better than a chain of `if`-`else`s.
>>
>
> As mentioned before in other thread, LLVM is crazy good at transforming if/else chains into switch statement, and GCC does a good job at it as well.
A `switch` statement is faster than a `if`-`else` chain only when it can use a lookup table, and for the lookup table a switch expression is a must. Another precondition for a lookup table is that the test expressions are known at compile-time.
The given example has none - there is no switch expression(though one may argue that you can use `true` as the switch expression) and the test expressions are function calls calculated at runtime.
If the compiler can translate a `if`-`else` chain to a `switch` statement - then it should have probably been a `switch` statement in the first place. That way it would have also been more readable.
|
April 18, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On 04/18/2013 04:18 PM, Idan Arye wrote:
> On Thursday, 18 April 2013 at 13:04:19 UTC, Graham Fawcett wrote:
>> On Sunday, 14 April 2013 at 23:52:37 UTC, Idan Arye wrote:
>>>
>>> Now, I've got it working(the example actually compiles and prints
>>> "less then 5"), and I want to make a pull request to put it in
>>> Phobos(because it's a useful function) but I don't really know where
>>> to put it. None of the existing modules seems fit, and I don't want
>>> to open a new module(std.monad?) for a single function.
>>
>> Nit-picky observation: there's nothing monadic about your predSwitch
>> function, it wouldn't belong in a 'std.monad' module.
>>
>> Best,
>> Graham
>
> A monad is "a structure that represents computations"(quote from
> Wikipedia). ...
Read on.
|
April 19, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Thursday, 18 April 2013 at 17:12:51 UTC, Timon Gehr wrote:
> On 04/18/2013 04:18 PM, Idan Arye wrote:
>> On Thursday, 18 April 2013 at 13:04:19 UTC, Graham Fawcett wrote:
>>> On Sunday, 14 April 2013 at 23:52:37 UTC, Idan Arye wrote:
>>>>
>>>> Now, I've got it working(the example actually compiles and prints
>>>> "less then 5"), and I want to make a pull request to put it in
>>>> Phobos(because it's a useful function) but I don't really know where
>>>> to put it. None of the existing modules seems fit, and I don't want
>>>> to open a new module(std.monad?) for a single function.
>>>
>>> Nit-picky observation: there's nothing monadic about your predSwitch
>>> function, it wouldn't belong in a 'std.monad' module.
>>>
>>> Best,
>>> Graham
>>
>> A monad is "a structure that represents computations"(quote from
>> Wikipedia). ...
>
> Read on.
OK, so it's not a formal monad, but it has the same idea of - "an action that chooses the next action based on the results of previous actions" - for the same purpose - "insert additional operations around a program's domain logic".
|
April 19, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On Friday, 19 April 2013 at 19:02:51 UTC, Idan Arye wrote:
> OK, so it's not a formal monad, but it has the same idea of - "an action that chooses the next action based on the results of previous actions" - for the same purpose - "insert additional operations around a program's domain logic".
Monad don't perform action. They describe an action to be performed by something else. Usually a lib or the runtime (as in Haskell).
|
April 19, 2013 Re: Where should I put a `condp` like function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Friday, 19 April 2013 at 19:05:10 UTC, deadalnix wrote:
> On Friday, 19 April 2013 at 19:02:51 UTC, Idan Arye wrote:
>> OK, so it's not a formal monad, but it has the same idea of - "an action that chooses the next action based on the results of previous actions" - for the same purpose - "insert additional operations around a program's domain logic".
>
> Monad don't perform action. They describe an action to be performed by something else. Usually a lib or the runtime (as in Haskell).
Oh, I see. So I guess I got it wrong...
|
Copyright © 1999-2021 by the D Language Foundation