Skip to content

Commit

Permalink
Use vim.iter():map()
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Apr 23, 2024
1 parent 65daae9 commit 5748d11
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
45 changes: 30 additions & 15 deletions lua/piemenu/core/angle_range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ function AngleRanges.new_one(s, e)
end

function AngleRanges.from_raw(raw)
return AngleRanges.new(vim.tbl_map(function(r)
return AngleRange.new(unpack(r))
end, raw))
return AngleRanges.new(vim
.iter(raw)
:map(function(r)
return AngleRange.new(unpack(r))
end)
:totable())
end

function AngleRanges.__index(self, k)
Expand All @@ -32,15 +35,21 @@ function AngleRanges.__index(self, k)
end

function AngleRanges.raw(self)
return vim.tbl_map(function(angle_range)
return { angle_range:raw() }
end, self._angle_ranges)
return vim
.iter(self._angle_ranges)
:map(function(angle_range)
return { angle_range:raw() }
end)
:totable()
end

function AngleRanges.list(self)
return vim.tbl_map(function(angle_range)
return angle_range
end, self._angle_ranges)
return vim
.iter(self._angle_ranges)
:map(function(angle_range)
return angle_range
end)
:totable()
end

function AngleRanges.join(self)
Expand All @@ -65,15 +74,21 @@ function AngleRanges.join(self)
table.remove(new_angle_ranges, #new_angle_ranges)
new_angle_ranges[1][1] = last_start_angle

return AngleRanges.new(vim.tbl_map(function(angles)
return AngleRange.new(unpack(angles))
end, new_angle_ranges))
return AngleRanges.new(vim
.iter(new_angle_ranges)
:map(function(angles)
return AngleRange.new(unpack(angles))
end)
:totable())
end

function AngleRanges.distances(self)
return vim.tbl_map(function(angle_range)
return angle_range:distance()
end, self._angle_ranges)
return vim
.iter(self._angle_ranges)
:map(function(angle_range)
return angle_range:distance()
end)
:totable()
end

function AngleRanges.exclude(self, angle_ranges)
Expand Down
36 changes: 24 additions & 12 deletions lua/piemenu/core/angle_splitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ function AngleSplitter.split(self)
vim.list_extend(angles, self:_split(start_angle, end_angle, count))
end

angles = vim.tbl_map(function(angle)
return Angle.new_0_to_359(angle)
end, angles)
angles = vim
.iter(angles)
:map(function(angle)
return Angle.new_0_to_359(angle)
end)
:totable()

local is_start
local start_angle = Angle.new_0_to_359(self._start_angle)
Expand Down Expand Up @@ -59,20 +62,29 @@ function AngleSplitter._split(_, start_angle, end_angle, count)
-- ex. if count == 2, distance == 330, space is insufficient between angle_a and angle_b.
increment_angle = angle_distance / count
end
return vim.tbl_map(function(i)
return start_angle + increment_angle * i
end, vim.fn.range(count))
return vim
.iter(vim.fn.range(count))
:map(function(i)
return start_angle + increment_angle * i
end)
:totable()
end

function AngleSplitter._counts(self, angles)
local angle_sum = listlib.sum(angles)
local count_rates = vim.tbl_map(function(angle)
return self._all_count * angle / angle_sum
end, angles)
local count_rates = vim
.iter(angles)
:map(function(angle)
return self._all_count * angle / angle_sum
end)
:totable()

local counts = vim.tbl_map(function(rate)
return math.floor(rate)
end, count_rates)
local counts = vim
.iter(count_rates)
:map(function(rate)
return math.floor(rate)
end)
:totable()

local remain_count = self._all_count - listlib.sum(counts)
if remain_count == 0 then
Expand Down

0 comments on commit 5748d11

Please sign in to comment.