James_E
In my code here, work_loop uses Enum.reduce_while/3, but as you can see it’s just discarding the accumulator.
I’m wondering if that’s a sign that there’s a more appropriate function for this?
import Bitwise
require Logger
# TS3H.demo("MEkDAgcAAgEgAh9n5rQmt3zJukGmwwvEVwfbZFS3NJ9oW7hgpJ0Db5ORAh8mOLmbP3+DfEQvEFbOcqymWoF6s9GHVcv8UegJBcWz", 1102362)
defmodule TS3H do
defp work_loop(intermed_state, goal, ctr_start, ctr_end) do
Enum.reduce_while(
ctr_start..ctr_end,
{nil, -1, -1},
fn ctr, _ ->
score = phase2(intermed_state, ctr)
if score < goal do
{:cont, {nil, ctr, score}}
else
{:halt, {:ok, ctr, score}}
end
end
)
end
def demo(pk_str, ctr_best) do
demo(pk_str, ctr_best, ctr_best)
end
def demo(pk_str, ctr_best, ctr_cur) do
intermed_state = phase1(pk_str)
# FIXME add a warning about bad keys / slow phase
record = phase2(intermed_state, ctr_best)
Logger.info("Current score: #{record}")
Logger.info("Skipping already checked: #{ctr_cur - ctr_best + 1}")
# Skip ctr_cur since that's *already been* checked
{outcome, ctr_last, last_score} = work_loop(intermed_state, record, ctr_cur + 1, ctr_cur + 2**24)
case outcome do
nil ->
Logger.notice("Got to #{ctr_last} without any progress.")
{record, ctr_best, ctr_last}
:ok ->
if last_score > record do
Logger.notice("Improved score!! #{ctr_last} -> #{last_score}")
{last_score, ctr_last, ctr_last}
else
Logger.notice("Found new head. #{ctr_last} -> #{record}")
{record, ctr_best, ctr_last}
end
end
end
def phase1(pk_str) do
:crypto.hash_init(:sha)
|> :crypto.hash_update(pk_str)
end
def phase2(state, ctr) do
:crypto.hash_update(state, Integer.to_string(ctr))
|> :crypto.hash_final()
|> ts3_binary_lzcnt()
end
def ts3_binary_lzcnt(hash) do # FIXME performance :(
# Octets are consumed in network order.
# Bits are consumed starting from the least significant bit.
# Examples:
# <<255, 0, 0, 0>> -> 0
# << 1, 0, 0, 0>> -> 0
# << 0, 1, 0, 0>> -> 8
# << 0, 255, 0, 0>> -> 8
# << 0, 254, 0, 0>> -> 9
Stream.unfold(hash, fn
# https://forum.elixirforum.com/t/trouble-understanding-octet-string-binary-iteration/62304
<<i::8, rem::binary>> -> {i, rem}
<<>> -> nil
end)
|> Enum.reduce_while(
{:cont, 0},
fn
0, {:cont, acc_val} -> {:cont, {:cont, acc_val + 8}}
x, {:cont, acc_val} -> {:cont, {:halt, acc_val + tzcnt(x, nil)}}
_, {:halt, acc_val} -> {:halt, {:halt, acc_val}}
end
)
|> elem(1)
end
defp tzcnt(i, nil, count) when i !== 0 do
# h/t https://programming-idioms.org/idiom/262/count-trailing-zero-bits/5157/erlang
if (i &&& 1) === 0 do
tzcnt(i >>> 1, nil, count + 1)
else
count
end
end
defp tzcnt(i, max) do
if i === 0 do
max
else
tzcnt(i, nil, 0)
end
end
end
The forum software correctly suggested that I take a look at thread #34178, but the suggestion there of using Enum.find_value/2 is an awkward fit because I like being able to return a single “semantically uniform” datatype rather than having special-cased shaped matches on the caller’s end.
I looked around the Enum documentation, but it’s really sprawling; there are so many unique functions, each filling a clear niche, and it’s possible I overlooked one that would fit my use-case.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
nicocirio
Hi!, I think recursion may be a good approach.
I haven’t tested it, but I think it works fine.
If you implement this approach, I suggest refactoring the args of work_loop for it to directly be the recursive function.
Eiji
It have “awkward” returns because the intention was to return them like that. You can return same type or different types based on pattern-matching - it’s up to you and your use case.
find_value/3simply discardsniland finishes as soon as it finds a value.Here goes a simplified example: