# Opportunity to write some idiomatic Julia code

**URL:** <https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702>\
**Category:** Community\
**Created:** [May 18, 2020, 4:14pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702 "2020-05-18T16:14:23Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![isene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isene/32/14981_2.png) [@isene](https://discourse.julialang.org/u/isene)\
**Post date:** [May 19, 2020, 1:01pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/22 "2020-05-19T13:01:47Z")

</div>

> I like this, this almost reads like Haskell ￼

I want to get into Haskell - would you like to make a Haskell-attempt on this?

Also what is the “Some” in "Some(“fumble”) do?

---

<div class="post-metadata">

**Author:** ![isene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isene/32/14981_2.png) [@isene](https://discourse.julialang.org/u/isene)\
**Post date:** [May 19, 2020, 1:03pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/23 "2020-05-19T13:03:57Z")

</div>

Damn, how do I blockquote correctly here (with the person I reply to included)?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 19, 2020, 1:48pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/24 "2020-05-19T13:48:44Z")

</div>

I’ll have a look but I am currently overloaded with other stuff 🙈

`Some` resembles an “option type”. Such things are quite usual in functional languages to model data which might or might not be there/returned. This way you can write fluid and type safe code (chaining functions into each other) without any explicit error checking layers.

---

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [May 19, 2020, 8:15pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/25 "2020-05-19T20:15:37Z")

</div>

I implemented the probabalistic implementation, including the modifier being a geometric distribution, here: [Opportunity to write some idiomatic Julia code - #11 by Ian\_Slagle](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/11) . The question of whether a “fumble” or “critical” happening is formulated as the chance of two heads being flipped in a row in a biased coin (1/3 for heads).

Since that, I reworked it to be a little smaller here: [Opportunity to write some idiomatic Julia code - #15 by Ian\_Slagle](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/15)

Using an Absorbing Markov Chain as its own type might be far more idiomatic, though

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 20, 2020, 4:33am UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/26 "2020-05-20T04:33:12Z")

</div>

> [@isene](#):
>
> Damn, how do I blockquote correctly here (with the person I reply to included)?

Just select the text you want to quote, and click the “Quote” button that pops up. See the “quoting” section in these [discourse tips](https://blog.discourse.org/2016/12/discourse-new-user-tips-and-tricks/).

---

<div class="post-metadata">

**Author:** ![isene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isene/32/14981_2.png) [@isene](https://discourse.julialang.org/u/isene)\
**Post date:** [May 20, 2020, 9:28pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/27 "2020-05-20T21:28:19Z")

</div>

Merged. Thanks.

---

<div class="post-metadata">

**Author:** ![isene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isene/32/14981_2.png) [@isene](https://discourse.julialang.org/u/isene)\
**Post date:** [May 20, 2020, 9:29pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/28 "2020-05-20T21:29:58Z")

</div>

I am impressed by the Julia community here. Thanks for the learning opportunity.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 21, 2020, 7:08am UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/29 "2020-05-21T07:08:36Z")

</div>

> [@Ian\_Slagle](#):
>
> Using an Absorbing Markov Chain as its own type might be far more idiomatic, though

I am not sure, that’s just brute-forcing the problem. I did not find a derivation of the formula you use [here](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/11), and currently I don’t have the bandwidth to play around with combinatorics (as much as I love to), but if there is a neat closed form, then the whole thing can be done like this:

1. first draw: branch on 1, 6, or anything else
2. 1 and 6 are symmetric, so only one of them needs to be handled. the number of additional draws n follows a geometric distribution with p = 1/2
3. if there is a closed-form expression for critical/fumble conditional on n, just generate a flag with that

In any case, this is just an interesting math exercise, and should not be considered a solution to the original one — it is my understanding that it is meant to display various languages coding the _same_ algorithm.

---

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [May 21, 2020, 3:17pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/30 "2020-05-21T15:17:53Z")

</div>

> [@Tamas\_Papp](#):
>
> - first draw: branch on 1, 6, or anything else
> - 1 and 6 are symmetric, so only one of them needs to be handled. the number of additional draws nn follows a geometric distribution with p=1/2p = 1/2
> - if there is a closed-form expression for critical/fumble conditional on nn , just generate a flag with that

Yeah that’s pretty much what my version does. I would like to derive that expression though, so that I can fully trust it (maybe that’s something I’ll get to today).

And that’s a fair point about the deviation from the main algorithm. I’m okay with it not being a “real” solution because it was an interesting thing to code, and some fun math. And I think it works out to be probabalistically the same.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2020, 9:10am UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/31 "2020-05-22T09:10:15Z")

</div>

I think the following should work: take a coin with probability p for H, 1-p for T. Let

1. A\_n denote the probability of having seen two consecutive H after n draws,
2. B\_n denote the probability of not having seen two consecutive H after n draws, but the last draw being H.

Then you can calculate these recursively as

A\_n = A\_{n-1} + B\_{n-1} \cdot p\\ B\_n = (1 - A\_{n-1} - B\_{n-1}) \cdot p

The first one follows from drawing H after a sequence ending with H, the second just follows from the coin being IID. The base case is A\_1 = 0, B\_1 = p.

One can either iterate this (still orders of magnitude cheaper than MC, and the calculation can be done exactly with `Rational{BigInt}`), or obtain a closed form using standard techniques. Something like

```julia
function critical(p, n)
    A, B = zero(p), p
    for i in 2:n
        @show A, B
        A, B = A + p * B, (1 - A - B) * p
    end
    A
end

julia> critical(1//2, 20)
(A, B) = (0//1, 1//2)
(A, B) = (1//4, 1//4)
(A, B) = (3//8, 1//4)
(A, B) = (1//2, 3//16)
(A, B) = (19//32, 5//32)
(A, B) = (43//64, 1//8)
(A, B) = (47//64, 13//128)
(A, B) = (201//256, 21//256)
(A, B) = (423//512, 17//256)
(A, B) = (55//64, 55//1024)
(A, B) = (1815//2048, 89//2048)
(A, B) = (3719//4096, 9//256)
(A, B) = (3791//4096, 233//8192)
(A, B) = (15397//16384, 377//16384)
(A, B) = (31171//32768, 305//16384)
(A, B) = (7869//8192, 987//65536)
(A, B) = (126891//131072, 1597//131072)
(A, B) = (255379//262144, 323//32768)
(A, B) = (256671//262144, 4181//524288)
1030865//1048576

```

(This may have bugs, or analytical mistakes, I didn’t spend a lot of time on it).

[Previous page](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702.md?page=1)
