Created
November 9, 2023 13:34
-
-
Save fonsp/fc7ace2bd8a0dfc2f87d694336f6c04a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### A Pluto.jl notebook ### | |
# v0.19.32 | |
using Markdown | |
using InteractiveUtils | |
# ╔═╡ 349fd1ca-7d1f-4150-9f6e-c0a5401f7d09 | |
using BenchmarkTools | |
# ╔═╡ 3062003c-7ef9-11ee-3894-4bb027622ad4 | |
begin | |
struct FunctionName | |
parts::Vector{Symbol} | |
end | |
Base.:(==)(a::FunctionName, b::FunctionName) = a.parts == b.parts | |
Base.hash(a::FunctionName, h::UInt) = hash(:FunctionName, hash(a.parts, h)) | |
end | |
# ╔═╡ e3d5b0f9-fe9b-4af2-8104-ca862f743c27 | |
join_funcname_parts(fn::FunctionName) = Symbol(join(fn.parts, '.')) | |
# ╔═╡ 0e08b9d7-8087-449d-be2a-fa67327d2956 | |
function generate_funcnames(fn::FunctionName) | |
calls = Vector{Vector{Symbol}}(undef, length(fn.parts) - 1) | |
for i = length(fn.parts):-1:2 | |
calls[i-1] = fn.parts[i:end] | |
end | |
calls | |
end | |
# ╔═╡ f5d4a2bd-c669-49f5-8d5f-bca488d863c2 | |
bes = FunctionName([:Base, :Experimental, :sqrt]) | |
# ╔═╡ ed7b934c-d100-4f38-82a8-34b950f8e55d | |
@benchmark FunctionName([:Base, :Experimental, :sqrt]) | |
# ╔═╡ 67fc8bfc-873d-47fd-ae28-6ba96fb1bc48 | |
# ╔═╡ e477167b-891e-4abb-b011-9a72e48fad11 | |
map(sqrt, (1,2,3)) | |
# ╔═╡ 5f19280f-6e72-4534-af59-21a8d6711e95 | |
function split_funcname(funcname_ex::Expr)::FunctionName | |
if funcname_ex.head == :(.) | |
out = FunctionName(Symbol[]) | |
args = funcname_ex.args | |
for arg in args | |
push!(out.parts, split_funcname(arg).parts...) | |
end | |
return out | |
else | |
# a call to a function that's not a global, like calling an array element: `funcs[12]()` | |
# TODO: explore symstate! | |
return FunctionName(Symbol[]) | |
end | |
end | |
# ╔═╡ 282bc479-2ba7-4fbb-8c39-6c9e11d9c14b | |
# ╔═╡ 3531acf6-662c-4f60-8a50-e65ea3f3569d | |
begin | |
"""Turn `Symbol(".+")` into `:(+)`""" | |
function without_dotprefix(funcname::Symbol)::Symbol | |
fn_str = String(funcname) | |
if length(fn_str) > 0 && fn_str[1] == '.' | |
Symbol(fn_str[2:end]) | |
else | |
funcname | |
end | |
end | |
"""Turn `Symbol("sqrt.")` into `:sqrt`""" | |
function without_dotsuffix(funcname::Symbol)::Symbol | |
fn_str = String(funcname) | |
if length(fn_str) > 0 && fn_str[end] == '.' | |
Symbol(fn_str[1:end-1]) | |
else | |
funcname | |
end | |
end | |
end | |
# ╔═╡ d2737a4e-e4cd-4b49-8871-8a2583ded827 | |
begin | |
function split_funcname(funcname_ex::QuoteNode)::FunctionName | |
split_funcname(funcname_ex.value) | |
end | |
function split_funcname(funcname_ex::GlobalRef)::FunctionName | |
split_funcname(funcname_ex.name) | |
end | |
function split_funcname(funcname_ex::Symbol)::FunctionName | |
FunctionName(Symbol[funcname_ex |> without_dotprefix |> without_dotsuffix]) | |
end | |
# this includes GlobalRef - it's fine that we don't recognise it, because you can't assign to a globalref? | |
function split_funcname(::Any)::FunctionName | |
FunctionName() | |
end | |
end | |
# ╔═╡ b294bfaf-2dbe-4b8d-94f1-b0b3c88d693b | |
const eee = :(Base.Experimental.sqrt) | |
# ╔═╡ 4f1bfd99-6fe1-4539-a3f7-22497cc32c3a | |
@benchmark $split_funcname(eee) | |
# ╔═╡ bdeb027f-45c8-4720-bde1-4d255305b37d | |
begin | |
struct FunctionNameTuple{N} | |
parts::NTuple{N,Symbol} | |
joined::Symbol | |
end | |
FunctionNameTuple(parts::Vector{Symbol}) = FunctionNameTuple( | |
tuple(parts...), | |
Symbol(join(parts, '.')), | |
) | |
FunctionNameTuple(parts::Symbol...) = FunctionNameTuple( | |
parts, | |
Symbol(join(parts, '.')), | |
) | |
FunctionNameTuple(part::Symbol) = FunctionNameTuple( | |
(part,), | |
part, | |
) | |
FunctionNameTuple() = FunctionNameTuple( | |
(), | |
Symbol(""), | |
) | |
# Base.:(==)(a::FunctionNameTuple, b::FunctionNameTuple) = a.parts == b.parts | |
end | |
# ╔═╡ 15152bb1-5622-45cc-bc81-2822db176e7d | |
join_funcname_parts(fn::FunctionNameTuple) = fn.joined | |
# ╔═╡ 937a6024-63b3-46b8-919e-6d7b9e566876 | |
@benchmark let | |
f = FunctionName([:Base, :Experimental, :sqrt]) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
end | |
# ╔═╡ 0649f016-87be-45b5-bc98-2d4dbb5f4995 | |
bes_t = FunctionNameTuple([:Base, :Experimental, :sqrt]) | |
# ╔═╡ 2ad8b28a-c71b-44c2-92d8-c875147a2157 | |
bes_t.parts === (:Base, :Experimental, :sqrt) | |
# ╔═╡ 627a7ecb-8dac-43a8-9bd9-7374083b36cd | |
@benchmark FunctionNameTuple([:Base, :Experimental, :sqrt]) | |
# ╔═╡ 68dc6e9d-8466-4a6e-a7cb-bc0840d007a7 | |
@benchmark let | |
f = FunctionNameTuple([:Base, :Experimental, :sqrt]) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
end | |
# ╔═╡ cbadec1c-aca0-4d60-87df-46cf1b9afd5c | |
@benchmark let | |
f = FunctionNameTuple(:Base, :Experimental, :sqrt) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
end | |
# ╔═╡ d86fa898-d88c-40e4-943d-f91a392a5431 | |
@benchmark let | |
f = FunctionNameTuple(:sqrt) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
end | |
# ╔═╡ 805f75db-21ec-409c-a976-52cf44d7d182 | |
@benchmark let | |
f = FunctionNameTuple() | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
join_funcname_parts(f) | |
end | |
# ╔═╡ 7e6d30f6-e314-4499-bce6-432e4cf8b72d | |
join_funcnames(x::FunctionNameTuple) = x | |
# ╔═╡ 2610c4fa-e5c1-489e-996f-30d53ba52981 | |
function join_funcnames(a::FunctionNameTuple, bs::FunctionNameTuple...) | |
b = join_funcnames(bs...) | |
FunctionNameTuple( | |
(a.parts..., b.parts...), | |
isempty(a.parts) ? b.joined : isempty(b.parts) ? a.joined : Symbol(a.joined, ".", b.joined) | |
) | |
end | |
# ╔═╡ 4e57a2ae-9c9b-4a3b-889b-618b31ca036d | |
function split_funcname_tuple(funcname_ex::Expr)::FunctionNameTuple | |
if funcname_ex.head == :(.) | |
mapfoldl(split_funcname_tuple, join_funcnames, funcname_ex.args; init=FunctionNameTuple()) | |
else | |
# a call to a function that's not a global, like calling an array element: `funcs[12]()` | |
# TODO: explore symstate! | |
return FunctionNameTuple() | |
end | |
end | |
# ╔═╡ 7854adb0-e9e2-4e9f-9a72-e68323bc7010 | |
begin | |
function split_funcname_tuple(funcname_ex::QuoteNode)::FunctionNameTuple | |
split_funcname_tuple(funcname_ex.value) | |
end | |
function split_funcname_tuple(funcname_ex::GlobalRef)::FunctionNameTuple | |
split_funcname_tuple(funcname_ex.name) | |
end | |
function split_funcname_tuple(funcname_ex::Symbol)::FunctionNameTuple | |
FunctionNameTuple(funcname_ex |> without_dotprefix |> without_dotsuffix) | |
end | |
# this includes GlobalRef - it's fine that we don't recognise it, because you can't assign to a globalref? | |
function split_funcname_tuple(::Any)::FunctionNameTuple | |
FunctionNameTuple() | |
end | |
end | |
# ╔═╡ 6c58f4d8-0bdc-4279-8965-bd37c6f6c034 | |
@benchmark $split_funcname_tuple(eee) | |
# ╔═╡ 503cff32-b84b-4887-a963-24292b743090 | |
split_funcname_tuple(eee) | |
# ╔═╡ dec65f8f-3ca0-4644-94e4-ed45999714a8 | |
QuoteNode |> fieldtypes | |
# ╔═╡ 24f3f752-a2eb-48d6-a002-f54e99ef3f61 | |
FunctionNameTuple(:a, :b) | |
# ╔═╡ 07142990-d125-4e43-92cb-dbec3c1a289c | |
function generate_funcnames(funccall::FunctionNameTuple) | |
calls = Vector{FunctionName}(undef, length(funccall) - 1) | |
for i = length(funccall):-1:2 | |
calls[i-1] = funccall[i:end] | |
end | |
calls | |
end | |
# ╔═╡ 1719412e-103a-47e5-9eb8-76cb904e86de | |
generate_funcnames() | |
# ╔═╡ 06eed0bb-d5c0-4c20-b261-9a2d338cde00 | |
NTuple | |
# ╔═╡ 9121b1b0-6efb-481c-8b46-684d42e25cbb | |
FunctionName([:a]) == FunctionName([:b]) | |
# ╔═╡ de9e2299-3f1c-4c66-854c-c181a58702f0 | |
FunctionNameTuple([:a]) == FunctionNameTuple([:b]) | |
# ╔═╡ 9ace2c30-4efc-47dd-9b67-e363b7ae4fc7 | |
FunctionName([:a]) == FunctionName([:a]) | |
# ╔═╡ 23fb913b-1059-4db8-b762-cc2960425f5f | |
FunctionNameTuple([:a]) == FunctionNameTuple([:a]) | |
# ╔═╡ 1d496d6c-e13c-4dec-82be-65414b294656 | |
hash(FunctionName([:a])) | |
# ╔═╡ f5f6b11a-01bc-4117-bc7d-fb32dd527aa2 | |
hash(FunctionName([:a])) | |
# ╔═╡ 20355a60-075d-4d4a-baa5-735dbc79f207 | |
hash(FunctionNameTuple([:a])) | |
# ╔═╡ 58b906e6-c264-4902-b384-018584546d88 | |
hash(FunctionNameTuple([:a])) | |
# ╔═╡ 0ce5c050-02f7-4e64-abc8-2c8cca01a4a3 | |
hash(FunctionName([:b])) | |
# ╔═╡ ae8fb88a-203e-463f-8f72-0a4286fb01f6 | |
hash(FunctionNameTuple([:b])) | |
# ╔═╡ 48884d6e-5735-446d-bba7-69725fc7f82b | |
join([:a, :b] |> FunctionName) | |
# ╔═╡ 8a0b1aa3-3ca4-4eae-ad36-bedc1b6f3ef4 | |
methods(join) | |
# ╔═╡ 00000000-0000-0000-0000-000000000001 | |
PLUTO_PROJECT_TOML_CONTENTS = """ | |
[deps] | |
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | |
[compat] | |
BenchmarkTools = "~1.3.2" | |
""" | |
# ╔═╡ 00000000-0000-0000-0000-000000000002 | |
PLUTO_MANIFEST_TOML_CONTENTS = """ | |
# This file is machine-generated - editing it directly is not advised | |
julia_version = "1.8.3" | |
manifest_format = "2.0" | |
project_hash = "8c72e043718b5b2b781afa164b5e4ec6fa6c9bde" | |
[[deps.Artifacts]] | |
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" | |
[[deps.BenchmarkTools]] | |
deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"] | |
git-tree-sha1 = "d9a9701b899b30332bbcb3e1679c41cce81fb0e8" | |
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | |
version = "1.3.2" | |
[[deps.CompilerSupportLibraries_jll]] | |
deps = ["Artifacts", "Libdl"] | |
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" | |
version = "0.5.2+0" | |
[[deps.Dates]] | |
deps = ["Printf"] | |
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" | |
[[deps.JSON]] | |
deps = ["Dates", "Mmap", "Parsers", "Unicode"] | |
git-tree-sha1 = "31e996f0a15c7b280ba9f76636b3ff9e2ae58c9a" | |
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" | |
version = "0.21.4" | |
[[deps.Libdl]] | |
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" | |
[[deps.LinearAlgebra]] | |
deps = ["Libdl", "libblastrampoline_jll"] | |
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | |
[[deps.Logging]] | |
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" | |
[[deps.Mmap]] | |
uuid = "a63ad114-7e13-5084-954f-fe012c677804" | |
[[deps.OpenBLAS_jll]] | |
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] | |
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" | |
version = "0.3.20+0" | |
[[deps.Parsers]] | |
deps = ["Dates", "PrecompileTools", "UUIDs"] | |
git-tree-sha1 = "716e24b21538abc91f6205fd1d8363f39b442851" | |
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" | |
version = "2.7.2" | |
[[deps.PrecompileTools]] | |
deps = ["Preferences"] | |
git-tree-sha1 = "03b4c25b43cb84cee5c90aa9b5ea0a78fd848d2f" | |
uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" | |
version = "1.2.0" | |
[[deps.Preferences]] | |
deps = ["TOML"] | |
git-tree-sha1 = "00805cd429dcb4870060ff49ef443486c262e38e" | |
uuid = "21216c6a-2e73-6563-6e65-726566657250" | |
version = "1.4.1" | |
[[deps.Printf]] | |
deps = ["Unicode"] | |
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" | |
[[deps.Profile]] | |
deps = ["Printf"] | |
uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" | |
[[deps.Random]] | |
deps = ["SHA", "Serialization"] | |
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | |
[[deps.SHA]] | |
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" | |
version = "0.7.0" | |
[[deps.Serialization]] | |
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" | |
[[deps.SparseArrays]] | |
deps = ["LinearAlgebra", "Random"] | |
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | |
[[deps.Statistics]] | |
deps = ["LinearAlgebra", "SparseArrays"] | |
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | |
[[deps.TOML]] | |
deps = ["Dates"] | |
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" | |
version = "1.0.0" | |
[[deps.UUIDs]] | |
deps = ["Random", "SHA"] | |
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" | |
[[deps.Unicode]] | |
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" | |
[[deps.libblastrampoline_jll]] | |
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] | |
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" | |
version = "5.1.1+0" | |
""" | |
# ╔═╡ Cell order: | |
# ╠═3062003c-7ef9-11ee-3894-4bb027622ad4 | |
# ╠═e3d5b0f9-fe9b-4af2-8104-ca862f743c27 | |
# ╠═15152bb1-5622-45cc-bc81-2822db176e7d | |
# ╠═0e08b9d7-8087-449d-be2a-fa67327d2956 | |
# ╠═f5d4a2bd-c669-49f5-8d5f-bca488d863c2 | |
# ╠═0649f016-87be-45b5-bc98-2d4dbb5f4995 | |
# ╠═2ad8b28a-c71b-44c2-92d8-c875147a2157 | |
# ╠═627a7ecb-8dac-43a8-9bd9-7374083b36cd | |
# ╠═68dc6e9d-8466-4a6e-a7cb-bc0840d007a7 | |
# ╠═cbadec1c-aca0-4d60-87df-46cf1b9afd5c | |
# ╠═d86fa898-d88c-40e4-943d-f91a392a5431 | |
# ╠═805f75db-21ec-409c-a976-52cf44d7d182 | |
# ╠═ed7b934c-d100-4f38-82a8-34b950f8e55d | |
# ╠═937a6024-63b3-46b8-919e-6d7b9e566876 | |
# ╠═67fc8bfc-873d-47fd-ae28-6ba96fb1bc48 | |
# ╠═1719412e-103a-47e5-9eb8-76cb904e86de | |
# ╠═e477167b-891e-4abb-b011-9a72e48fad11 | |
# ╠═5f19280f-6e72-4534-af59-21a8d6711e95 | |
# ╠═d2737a4e-e4cd-4b49-8871-8a2583ded827 | |
# ╠═4f1bfd99-6fe1-4539-a3f7-22497cc32c3a | |
# ╠═6c58f4d8-0bdc-4279-8965-bd37c6f6c034 | |
# ╠═503cff32-b84b-4887-a963-24292b743090 | |
# ╠═7e6d30f6-e314-4499-bce6-432e4cf8b72d | |
# ╠═2610c4fa-e5c1-489e-996f-30d53ba52981 | |
# ╠═282bc479-2ba7-4fbb-8c39-6c9e11d9c14b | |
# ╠═4e57a2ae-9c9b-4a3b-889b-618b31ca036d | |
# ╠═7854adb0-e9e2-4e9f-9a72-e68323bc7010 | |
# ╠═3531acf6-662c-4f60-8a50-e65ea3f3569d | |
# ╠═b294bfaf-2dbe-4b8d-94f1-b0b3c88d693b | |
# ╠═bdeb027f-45c8-4720-bde1-4d255305b37d | |
# ╠═dec65f8f-3ca0-4644-94e4-ed45999714a8 | |
# ╠═24f3f752-a2eb-48d6-a002-f54e99ef3f61 | |
# ╠═07142990-d125-4e43-92cb-dbec3c1a289c | |
# ╠═349fd1ca-7d1f-4150-9f6e-c0a5401f7d09 | |
# ╠═06eed0bb-d5c0-4c20-b261-9a2d338cde00 | |
# ╠═9121b1b0-6efb-481c-8b46-684d42e25cbb | |
# ╠═de9e2299-3f1c-4c66-854c-c181a58702f0 | |
# ╠═9ace2c30-4efc-47dd-9b67-e363b7ae4fc7 | |
# ╠═23fb913b-1059-4db8-b762-cc2960425f5f | |
# ╠═1d496d6c-e13c-4dec-82be-65414b294656 | |
# ╠═f5f6b11a-01bc-4117-bc7d-fb32dd527aa2 | |
# ╠═20355a60-075d-4d4a-baa5-735dbc79f207 | |
# ╠═58b906e6-c264-4902-b384-018584546d88 | |
# ╠═0ce5c050-02f7-4e64-abc8-2c8cca01a4a3 | |
# ╠═ae8fb88a-203e-463f-8f72-0a4286fb01f6 | |
# ╠═48884d6e-5735-446d-bba7-69725fc7f82b | |
# ╠═8a0b1aa3-3ca4-4eae-ad36-bedc1b6f3ef4 | |
# ╟─00000000-0000-0000-0000-000000000001 | |
# ╟─00000000-0000-0000-0000-000000000002 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment