« first day (1940 days earlier)      last day (2842 days later) » 

11:15
Quick question about creating 'chains', the chain is valid if all adjacent elements fulfill a condition. E.g. reorder a list of triplets where neighbours share exactly 2 elements. (assuming it is possible)
Is there a more idiomatic way to 'chain' elements by condition other than creating adjacency graph and finding hamiltonian path?
 
1 hour later…
12:21
@MichaelE2 ok, thanks for a comment, will take a closer look
12:52
0
Q: Migrated questions' links

KubaIntro Let's say there was a question on Mathematica.SE we migrated to Math.SE or somewhere. I could bet that the original topic marked [migrated] visible on the main in Mathematica.SE would automatically redirect to migrated on Math.SE. It does not seem to be the case anymore. More explanatio...

 
7 hours later…
20:00
What would be the best way to make a function that had a return that alternates between 1 and -1? So the first time it was called f[] gave 1, second time -1, the third 1 and so on..
20:16
@chuy Do you have absolutely no idea how to start?
Obviously, the key point is that your function needs to maintain an internal state that switches between each function call from 1 to -1 and back.
20:33
well I have an idea, but its hacky as hell
20:45
@halirutan sorry I was pulled away
here is what I have `f[] := ( c++; (-1)^c)` and so
```In[5]:= Table[f[], {10}]
Out[5]= {1, -1, 1, -1, 1, -1, 1, -1, 1, -1}```
@chuy There is one big issue with this code: c is a global variable. Do you know a way out?
I know thats the issue
I want to avoid having this global variable, but my mind is blanking on how.
@chuy Well, that is unfortunate because the solution is so simple, you won't believe it.
@chuy What do you do if you want a variable to be local?
(whatever local means)
I would use either Block or Module depending on my scoping needs
21:01
@chuy Let's go with Module: Do you see a way to localize c with Module?
f[] := Module[{c = $ModuleNumber}, (-1)^c]
Don't forget you can just make up a garbage context to put variables in, like
f[] := (notGlobalContextc++; (-1)^notGlobalContextc)
but surely thats not what you were hinting at
the little back ticks didn't come through
``` f[] := (notGlobalContextc++; (-1)^notGlobalContextc) ```
@chuy What I was hinting to is usually described in advanced usages of Module. Look at the following much simpler code:
21:07
Module[{c = -1},
 f[] := (c++; (-1)^c)
 ]
And you can read about this here
178
A: What are the use cases for different scoping constructs?

Leonid ShifrinYou will find a lot of information in this answer. I will add a few personal notes. Module Use Module when you want to localize variables inside your function's body, and those variables will potentially acquire and/or change their values during the computation. Basic use For example: f[x_]:...

@JasonB Your backticks should come through... f[] := (notGlobalContext`c++; (-1)^notGlobalContext`c)
closure thats the word
@chuy Exactly :)
my brain is mush these days
21:30
@halirutan thanks for the kick to the head!
@chuy No problem.
now I can answer a question (and give you due credit for making it less hacky)

« first day (1940 days earlier)      last day (2842 days later) »