# Comprehension, assignment clause

**URL:** https://discourse.julialang.org/t/comprehension-assignment-clause/5390
**Category:** New to Julia
**Created:** [August 15, 2017, 3:49pm UTC](https://discourse.julialang.org/t/comprehension-assignment-clause/5390 "2017-08-15T15:49:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![prairie-guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prairie-guy/32/1626_2.png) [@prairie-guy](https://discourse.julialang.org/u/prairie-guy)
#### Post date: [August 15, 2017, 3:49pm UTC](https://discourse.julialang.org/t/comprehension-assignment-clause/5390/1 "2017-08-15T15:49:29Z")

</div>

Big fan of Julia! My other favorite language is Clojure. In both languages, comprehensions are my goto tool for many purposes. In Clojure, comprehensions provide an assignment feature that I find very useful. Here is an example:

```julia
(for [x [0 1 2 3 4 5]
      :let [y (* x 3)]
      :when (even? y)]
  y)

```

---

<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: [August 15, 2017, 4:56pm UTC](https://discourse.julialang.org/t/comprehension-assignment-clause/5390/3 "2017-08-15T16:56:07Z")

</div>

You can use a `let` block inside a comprehension in Julia:

```julia
julia> [let y = x*3; y^3; end for x in 0:5]
6-element Array{Int64,1}:
    0
   27
  216
  729
 1728
 3375

```

---

<div class="post-metadata">

### Author: ![prairie-guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prairie-guy/32/1626_2.png) [@prairie-guy](https://discourse.julialang.org/u/prairie-guy)
#### Post date: [August 15, 2017, 5:43pm UTC](https://discourse.julialang.org/t/comprehension-assignment-clause/5390/4 "2017-08-15T17:43:06Z")

</div>

Thanks for your prompt feedback. I’m probably missing something, but here is a sample of existing code:

```julia
[f(i,j) for i=1:mx for j=i+1:mx if f(i,j) < n && iseven(f(i,j)))])

```

Assuming f(i,j) is expensive to run, I would like to just call it once. In Clojure, this is accomplished with the :let symbol. I can clearly do it with a `for` statement, but I like the elegance of comprehensions.

Thanks.

---

<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: [August 15, 2017, 5:51pm UTC](https://discourse.julialang.org/t/comprehension-assignment-clause/5390/5 "2017-08-15T17:51:11Z")

</div>

If a value is expensive and is used in both the result and the filter condition, you can first calculate the value, then filter on it (ie two comprehensions, one inside the other).
