From 3a2dcc0bc1c09ef2ad866de36f2858d8b3403148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Ma=C3=B1osa?= <26429103+albertms10@users.noreply.github.com> Date: Thu, 18 Apr 2024 02:22:31 +0200 Subject: [PATCH] =?UTF-8?q?refactor(scale=5Fpattern):=20=E2=99=BB=EF=B8=8F?= =?UTF-8?q?=20extract=20bit-related=20methods=20into=20`BinarySequence`=20?= =?UTF-8?q?extension=20(#479)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Albert MaƱosa <26429103+albertms10@users.noreply.github.com> --- lib/src/scale/scale_pattern.dart | 30 +++++++++++++++----------- test/src/scale/scale_pattern_test.dart | 5 ----- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/src/scale/scale_pattern.dart b/lib/src/scale/scale_pattern.dart index 3deaeabe..e510c745 100644 --- a/lib/src/scale/scale_pattern.dart +++ b/lib/src/scale/scale_pattern.dart @@ -250,19 +250,12 @@ final class ScalePattern { return major; } - static int _bitAt(int sequence, int index) => sequence & 1 << index; - - static int _toBit(int sequence, Scalable scalable) => - sequence | 1 << scalable.semitones; - /// Creates a new [ScalePattern] from a binary [sequence] in integer form. /// /// This method and [ScalePattern.toBinary] are inverses of each other. /// /// Example: /// ```dart - /// extension on int { int get b => int.parse(toString(), radix: 2); } - /// /// ScalePattern.fromBinary(101010110101.b) == ScalePattern.major /// ScalePattern.fromBinary(111111111111.b) == ScalePattern.chromatic /// ScalePattern.fromBinary(1010010101.b) == ScalePattern.majorPentatonic @@ -274,7 +267,7 @@ final class ScalePattern { final degrees = [ for (int i = 0; i < chromaticDivisions; i++) - if (_bitAt(sequence, i) != 0) PitchClass(i), + if (sequence.bitAt(i) != 0) PitchClass(i), PitchClass.c, ]; final descendingDegrees = descendingSequence == null @@ -282,7 +275,7 @@ final class ScalePattern { : [ PitchClass.c, for (int i = chromaticDivisions - 1; i >= 0; i--) - if (_bitAt(descendingSequence, i) != 0) PitchClass(i), + if (descendingSequence.bitAt(i) != 0) PitchClass(i), ]; return Scale(degrees, descendingDegrees).pattern; @@ -294,8 +287,6 @@ final class ScalePattern { /// /// Example: /// ```dart - /// extension on int { int get b => int.parse(toString(), radix: 2); } - /// /// ScalePattern.major.toBinary() == (101010110101.b, null) /// ScalePattern.chromatic.toBinary() == (111111111111.b, null) /// ScalePattern.majorPentatonic.toBinary() == (1010010101.b, null) @@ -303,12 +294,12 @@ final class ScalePattern { /// ``` (int sequence, int? descendingSequence) toBinary() { final scale = on(PitchClass.c); - final sequence = scale.degrees.fold(0, _toBit); + final sequence = scale.degrees.fold(0, BinarySequence.bitFrom); final cachedDescending = scale.descendingDegrees; final descendingSequence = cachedDescending.reversed.isEnharmonicWith(scale.degrees) ? null - : cachedDescending.fold(0, _toBit); + : cachedDescending.fold(0, BinarySequence.bitFrom); return (sequence, descendingSequence); } @@ -480,3 +471,16 @@ final class ScalePattern { : null, ); } + +/// A binary sequence int extension. +extension BinarySequence on int { + /// The bit at [index]. + int bitAt(int index) => this & 1 << index; + + /// The bit from [sequence] and [scalable] semitones. + static int bitFrom(int sequence, Scalable scalable) => + sequence | 1 << scalable.semitones; + + /// This [int] as a binary integer. + int get b => int.parse(toString(), radix: 2); +} diff --git a/test/src/scale/scale_pattern_test.dart b/test/src/scale/scale_pattern_test.dart index 92b80ed0..b42da142 100644 --- a/test/src/scale/scale_pattern_test.dart +++ b/test/src/scale/scale_pattern_test.dart @@ -666,8 +666,3 @@ void main() { }); }); } - -extension on int { - /// Parse this [String] as a binary integer. - int get b => int.parse(toString(), radix: 2); -}